Skip to content

Instantly share code, notes, and snippets.

View frgomes's full-sized avatar

Richard Gomes frgomes

View GitHub Profile
object TypelevelPolymorphic extends App {
object square extends Poly {
import scala.language.reflectiveCalls
implicit val is = at[Int] { i => i * i }
implicit val fs = at[Float] { f => f * f }
implicit val ds = at[Double] { d => d * d }
}
trait Poly {
def escapeNonASCII(str: String) : String = {
val result = new java.lang.StringBuilder
var i = 0
while(i < str.length) {
val cp: Int = Character.codePointAt(str, i)
val n : Int = Character.charCount(cp)
if(n > 1) {
i += n-1
if(i >= str.length) throw new IllegalArgumentException("truncated unexpectedly")
}
def addNaturals(nats: List[Int]): Int = {
require(nats forall (_ >= 0), "List contains negative numbers")
nats.foldLeft(0)(_ + _)
} ensuring(_ >= 0)
organization := "mathminds"
name := "java-only-project"
scalaVersion in ThisBuild := "2.12.8"
scalacOptions in ThisBuild += "-target:jvm-1.8"
javacOptions in ThisBuild ++= Seq("-source", "1.8", "-target", "1.8")
// Enables publishing to maven repo
publishMavenStyle := true
@frgomes
frgomes / SpanGroups.scala
Last active December 11, 2018 12:45
scala - Count elements per category
/** Group objects in categories. Requires ordered input. */
def groups[A, K](s: Iterable[A], acc: Stream[Iterable[A]] = Stream.empty[Iterable[A]])
(implicit groupKeyOf: (A => K)): Iterable[Iterable[A]] = {
s match {
case first :: _ =>
val (left, right) = s.span(p => groupKeyOf(p) == groupKeyOf(first))
groups(right, acc :+ left)(groupKeyOf)
case Nil => acc
}
@frgomes
frgomes / OptionalSyntax.scala
Last active December 5, 2018 23:38
scala - convenience implicit conversions for optional types
package rgomes.info.lang
import utest._
object OptionalSyntaxSpec extends TestSuite {
trait OptionalWrapper[T] {
def wrap(data: T): Option[T]
}
@frgomes
frgomes / rollback-reboot
Last active October 5, 2022 22:30
etckeeper - rollback and reboot in case of not committed changes
#!/bin/bash
sleep 300
[[ -d /etc/.git ]] && cd /etc && \
[[ $(git status --short | wc -l) -gt 0 ]] && \
git stash && \
logger etckeeper-roolback: reverted /etc to a previous state && \
sync; sync; sync && \
reboot
@frgomes
frgomes / digitalocean_functions.sh
Last active August 29, 2018 19:30
cloud-init - template for creating a droplet in Digital Ocean
function digitalocean_json_droplet_create {
local label=$1
local hostname=$2
local domainname=$3
local region=$4
local tags=$5
local size=$6
local image=$7
local tags=$(echo "${tags}" | digitalocean_json_from_csv)
@frgomes
frgomes / fqdn.scala
Last active May 20, 2020 16:52
Scala - Obtain FQDN
def fqdn: Option[String] = {
val marker = "domain "
def ipv4(canonical: String): Boolean = {
def isAllDigits(x: String) = x forall Character.isDigit
canonical != null && !canonical.isEmpty && {
val parts = canonical.split('.')
parts.length > 1 && !parts.map(part => isAllDigits(part)).reduce(_ & _)
}
}
val addr: java.net.InetAddress = java.net.InetAddress.getLocalHost
@frgomes
frgomes / ddns-update
Last active January 15, 2022 17:16
bash - sysadm - /usr/sbin/ddns-update
#!/bin/bash -eu
#######################################################################
## ##
## A simple, lightweight and self contained alternative to ddclient. ##
## ##
#######################################################################
#
# Configuration