This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def splitWhen[A](list: List[A], f: A => Boolean): List[List[A]] = { | |
list.reverse.foldLeft((List.empty[List[A]], true)) { | |
case ((Nil, _), e) if f(e) => (Nil, true) | |
case ((Nil, _), e) => (List(List(e)), false) | |
case ((head :: tail, _), e) if f(e) => (head :: tail, true) | |
case ((head :: tail, false), e) => ((e :: head) :: tail, false) | |
case ((head :: tail, true), e) => (List(e) :: head :: tail, false) | |
}._1 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import $ivy.`org.typelevel::cats-core:2.3.0` | |
import cats.data.NonEmptyList | |
import cats.Foldable | |
import cats.implicits._ | |
def sliding2[F[_] : Foldable, A](l: F[A]): Option[NonEmptyList[(A, A)]] = { | |
l.foldLeft[(List[(A, A)], Option[A])]((Nil, None)) { | |
case ((acc, Some(x1)), x2) => | |
((x1, x2) :: acc, Some(x2)) | |
case ((Nil, None), x) => |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import $ivy.`org.typelevel::cats-core:2.3.0` | |
def find2Addends(numbers: List[Long], sum: Long): Option[(Long, Long)] = { | |
numbers.collectFirstSome { x => | |
val y = sum - x | |
if (numbers.contains(y)) Some((x, y)) | |
else None | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://github.com/apache/lucene-solr/blob/master/lucene/queryparser/src/java/org/apache/lucene/queryparser/classic/QueryParserBase.java#L973 | |
def luceneEscape(s: String): String = { | |
// use string builder, as it's probably more efficient | |
val sb = new StringBuilder | |
s.foreach { | |
case char @ ('\\' | '+' | '-' | '!' | '(' | ')' | ':' | '^' | '[' | ']' | '\"' | '{' | '}' | '~' | '*' | '?' | '|' | '&' | '/') => | |
sb.append('\\').append(char) | |
case other => sb.append(other) | |
} | |
sb.result() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -euo pipefail | |
echoerr() { echo "$@" 1>&2; } | |
if [[ $# -ne 1 ]]; then | |
echoerr "Usage: $0 file" | |
exit 1 | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// publish to your maven repo using `sbt "myProject/publish"` | |
// note maven appears to only really support a single jar per library, so create multiple sub-modules | |
// name of the jar in modules/my-project/lib/<name>.jar | |
val jarName = "my-vendor-jar" | |
lazy val myJar = (project in file("modules/my-jar")) | |
.settings( | |
name := "my-jar", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Evaluate simple math expressions, supports add/times. | |
* Operator precedence is observed. | |
* e.g. in the expression 5+3*2, 3*2 would be evaluated first, leading to the final result of 11. | |
*/ | |
def eval(expr: String): Int = { | |
var i = 0 | |
var result = 0 | |
var num = 0 | |
while (i < expr.size) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env amm | |
@main | |
def main(n: BigInt): Unit = | |
println(num2wordList(n, List.empty).mkString(" ")) | |
// https://en.wikipedia.org/wiki/Names_of_large_numbers | |
private val prefix2factor = List( | |
"Centillion" -> 303, | |
"Googol" -> 100, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.UUID | |
import org.http4s.{Header, Http, Request, Response} | |
import cats.data.Kleisli | |
import cats.effect.Sync | |
import cats.implicits._ | |
import org.http4s.syntax.string._ | |
/** | |
* Propagate a `X-Request-Id` header to the response, generate a UUID |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.gubbns | |
import caseapp.core.Error | |
import caseapp.core.help.{Help, WithHelp} | |
import caseapp.core.parser.Parser | |
import caseapp.core.RemainingArgs | |
import cats.effect.{ExitCode, IO, IOApp} | |
/** IO version of CaseApp. */ | |
abstract class IOCaseApp[T](implicit val parser: Parser[T], val messages: Help[T]) extends IOApp { |
NewerOlder