Skip to content

Instantly share code, notes, and snippets.

View paulmr's full-sized avatar

Paul Roberts paulmr

  • Guardian
  • London
View GitHub Profile
object ExampleScala {
def calc(p: Int) = {
var result = 0
for {
a <- 3 to p
b <- 1 until ((p - a) / 2)
c = p - (a + b)
} if (b * b + c * c == a * a) result += 1
result
}
@paulmr
paulmr / timer.scala
Created June 14, 2021 14:38
time a scala expression's execution
// e.g., from the repl:
// scala> :load timer.scala
// [...]
// scala> timed(Thread.sleep(1000))
// took 1001606189ns (1001ms)
def timed[T](block: => T): T = {
import java.time.Duration
val startTime = System.nanoTime()
@paulmr
paulmr / timed.nim
Created May 12, 2021 22:04
simple little profiler in nim
import tables, strformat, std/monotimes, times
var profile = initTable[string, Duration]()
var totalStart = getMonoTime()
template timed*(name: string, it: untyped): untyped =
let start = getMonoTime()
it
let took = getMonoTime() - start
@paulmr
paulmr / es.sc
Created September 9, 2019 16:19
more reproducing
import $ivy.`org.elasticsearch:elasticsearch:6.8.0`
import $ivy.`org.elasticsearch.client:transport:6.8.0`
// import $ivy.`org.elasticsearch:elasticsearch:7.3.1`
// import $ivy.`org.elasticsearch.client:transport:7.3.1`
import org.elasticsearch.common.xcontent.XContentType
import org.elasticsearch.common.transport.TransportAddress
import org.elasticsearch.transport.client.PreBuiltTransportClient
import org.elasticsearch.common.settings.Settings
diff --git a/api/src/scala/scaled/major/EditingMode.scala b/api/src/scala/scaled/major/EditingMode.scala
index 96171fb..c22afe7 100644
--- a/api/src/scala/scaled/major/EditingMode.scala
+++ b/api/src/scala/scaled/major/EditingMode.scala
@@ -40,7 +40,8 @@ abstract class EditingMode (env :Env) extends ReadingMode(env) {
bind("newline", "ENTER", "S-ENTER").
bind("indent-for-tab-command", "TAB").
- // TODO: open-line, split-line, ...
+ bind("open-line", "C-o").
@paulmr
paulmr / check_date_format.scala
Created January 3, 2019 11:44
compare many dates to ensure the same result when comparing two formatters
import org.joda.time.DateTime
import org.joda.time.format.{ DateTimeFormat, DateTimeFormatter, ISODateTimeFormat }
def fmt(df: DateTimeFormatter) = (d: DateTime) => df.print(d)
def patt(p: String) = fmt(DateTimeFormat.forPattern(p))
@annotation.tailrec
def compare(startDate: DateTime, endDate: DateTime, f1: DateTime => String, f2: DateTime => String, count: Int = 0): Boolean = {
if(startDate.isAfter(endDate)) {
println(s"Checked: $count")
@paulmr
paulmr / api_compare.py
Last active December 13, 2018 17:03
tool for comparing the results of two json APIs
import requests, json, re, yaml
import logging
from urllib.parse import urlparse, urljoin, parse_qs
from datetime import datetime, timedelta
verbose = True
logfile = None
hosts = []
@paulmr
paulmr / fastparse.org
Created December 3, 2018 12:18
Code from the live demo of FastParse during my Scala in the City talk

FastParse Live Demo, Scala In The City November 2018

Import the Fast parse library

As well as the unsurprising fastparse._ we also need to import some implicits which affect the behaviour of the repetition operators. This is because there are multiple ways to define repetition, for example whether or not you want to ignore whitespace and so in any given context you can have implicits which customise this behaviour without having to change your code.

@paulmr
paulmr / example.json
Last active November 28, 2018 16:34
Some tests for running a few different Scala JSON parsers on the same input data
[
{
"id": 0,
"guid": "c4d14538-e09f-4eeb-b877-1874c5a42b25",
"isActive": true,
"balance": "$2,635.00",
"picture": "http://placehold.it/32x32",
"age": 40,
"name": "Powers Dixon",
"gender": "male",
@paulmr
paulmr / simple-json.sc
Created October 8, 2018 10:53
simple json example (using uJson)
val personJson = """| {
| "name": "Clark Kent",
| "age": 24
| }""".stripMargin
case class Person(name: String, age: Int)
val input = ujson.read(personJson)