Skip to content

Instantly share code, notes, and snippets.

View malcolmgreaves's full-sized avatar

Malcolm Greaves malcolmgreaves

View GitHub Profile
@malcolmgreaves
malcolmgreaves / FunctionToFlow.scala
Created October 14, 2015 01:41
Convert generic function to Akka Streams Graph with FlowShape
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 October 16, 2015 21:51 — forked from mefellows/ scala-travis-ci-sonatype-yml
Scala - Travis CI Publish Sonatype YAML Sample
language: scala
scala:
- 2.10.4
jdk:
- oraclejdk8
- oraclejdk7
services: mongodb
script:
- sbt scoverage:test
- sbt coveralls
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 March 2, 2016 20:16 — forked from loicdescotte/Forcomptran.md
Scala for comprehension translation helper

#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 March 4, 2016 19:14 — forked from rob-murray/add_intellij_launcer
Add Intellij launcher shortcut and icon for ubuntu
// 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 April 7, 2016 00:07
Perform a side-effecting function on an Option's value and allow for chaining.
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 April 7, 2016 00:06
Easily convert a Try[T] into a scalaz.\/[Throwable, T]
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 April 10, 2016 20:12
Adds a convenient method to perform a side effect and evaluate to the (originally called) value.
implicit class ChainSideEffect[T](private val x: T) extends AnyVal {
@inline def sideEffect(op: () => Unit): T = {
val _: Unit = op()
x
}
}
@malcolmgreaves
malcolmgreaves / umbrella
Created July 21, 2016 17:44 — 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.
#!/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 August 2, 2016 22:28
Nullable is a class whose type safety is _intentionally_ compromised for the sake of efficiency on the JVM.
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.