Skip to content

Instantly share code, notes, and snippets.

Avatar

Malcolm Greaves malcolmgreaves

View GitHub Profile
@malcolmgreaves
malcolmgreaves / FunctionToFlow.scala
Created Oct 14, 2015
Convert generic function to Akka Streams Graph with FlowShape
View FunctionToFlow.scala
object FunctionToFlow {
import akka.stream._
import akka.stream.scaladsl.{ FlowGraph, Flow }
def apply[A, B](f: A => B): Graph[FlowShape[A, B], Unit] =
FlowGraph.partial() { implicit b =>
val transformation = b.add(Flow[A].map(f))
new FlowShape[A, B](
transformation.inlet,
@malcolmgreaves
malcolmgreaves / scala-travis-ci-sonatype-yml
Created Oct 16, 2015 — forked from mefellows/ scala-travis-ci-sonatype-yml
Scala - Travis CI Publish Sonatype YAML Sample
View scala-travis-ci-sonatype-yml
language: scala
scala:
- 2.10.4
jdk:
- oraclejdk8
- oraclejdk7
services: mongodb
script:
- sbt scoverage:test
- sbt coveralls
View DiffieHellmanMerkle.scala
object DiffieHellmanMerkle {
import java.math.BigInteger
import scala.language.implicitConversions
import scala.util.Random
def main(args: Array[String]): Unit =
println(
diffieHellmanMerkle(generator = 3, modulus = 17, alicePrivateKey = 54, bobPrivateKey = 24)
)
@malcolmgreaves
malcolmgreaves / Forcomptran.md
Created Mar 2, 2016 — forked from loicdescotte/Forcomptran.md
Scala for comprehension translation helper
View Forcomptran.md

#Scala for comprehension translation helper

"For comprehension" is a another syntaxe to use map, flatMap and withFilter (or filter) methods.

yield keyword is used to aggregate values in the resulting structure.

This composition can be used on any type implementing this methods, like List, Option, Future...

@malcolmgreaves
malcolmgreaves / add_intellij_launcer
Created Mar 4, 2016 — forked from rob-murray/add_intellij_launcer
Add Intellij launcher shortcut and icon for ubuntu
View add_intellij_launcer
// create file:
sudo vim /usr/share/applications/intellij.desktop
// add the following
[Desktop Entry]
Version=13.0
Type=Application
Terminal=false
Icon[en_US]=/home/rob/.intellij-13/bin/idea.png
Name[en_US]=IntelliJ
@malcolmgreaves
malcolmgreaves / AddSideEffectOpToOption.scala
Last active Apr 7, 2016
Perform a side-effecting function on an Option's value and allow for chaining.
View AddSideEffectOpToOption.scala
implicit class AddSideEffectOpToOption[T](private val x: Option[T]) extends AnyVal {
@inline def sideEffectOnly(ifNone: => Unit, ifSome: T => Unit): Option[T] = {
x match {
case None => ifNone
case Some(value) => ifSome(value)
}
x
}
@malcolmgreaves
malcolmgreaves / TryToDisjunction.scala
Created Apr 7, 2016
Easily convert a Try[T] into a scalaz.\/[Throwable, T]
View TryToDisjunction.scala
implicit class TryToDisjunction[T](private val x: Try[T]) extends AnyVal {
@inline def toOr: \/[Throwable, T] =
x match {
case Success(value) => \/-(value)
case Failure(e) => -\/(e)
}
}
@malcolmgreaves
malcolmgreaves / ChainSideEffect.scala
Created Apr 10, 2016
Adds a convenient method to perform a side effect and evaluate to the (originally called) value.
View ChainSideEffect.scala
implicit class ChainSideEffect[T](private val x: T) extends AnyVal {
@inline def sideEffect(op: () => Unit): T = {
val _: Unit = op()
x
}
}
@malcolmgreaves
malcolmgreaves / umbrella
Created Jul 21, 2016 — forked from quinncomendant/umbrella
OpenDNS umbrella start/stop script for Mac OS X. This makes it easy to turn umbrella off temporarily, or get its status.
View umbrella
#!/usr/bin/env bash
# Quinn Comendant <quinn@strangecode.com>
# https://gist.github.com/quinncomendant/3be731567e529415d5ee
# Since 25 Jan 2015
# Version 1.1
CMD=$1;
if [[ `id -u` = 0 ]]; then
@malcolmgreaves
malcolmgreaves / Nullable.scala
Created Aug 2, 2016
Nullable is a class whose type safety is _intentionally_ compromised for the sake of efficiency on the JVM.
View Nullable.scala
import scala.language.implicitConversions
/**
* SPECIAL SEMANTICS - USE WITH CAUTION
*
* Nullable is a class whose type safety is _intentionally_ compromised. In
* performance-critical code, we do not want to allocate an Option wrapper
* around our value of type T. Here we use an extension of AnyVal to ensure
* we do not allocate and provide inlined helper functions to check the
* underlying value's nullity.