Skip to content

Instantly share code, notes, and snippets.

View justinhj's full-sized avatar

Justin Heyes-Jones justinhj

View GitHub Profile
@justinhj
justinhj / RomanConvert.elm
Created August 15, 2018 17:30
Convert numbers to a string of Roman Numerals and back using the Elm language
module RomanConvert exposing (..)
import Dict exposing (Dict, fromList)
import List.Extra exposing (find)
import Set exposing (..)
-- Elm implementation of converting to and from Integer and a string representation of a Roman Numeral
-- (C)2018 Justin Heyes-Jones
-- These are the valid characters that make up Roman Numerals
@justinhj
justinhj / tl1.scala
Created September 29, 2018 18:01
Example of using a type lambda for implementing Functor over Maps
// Functor
trait JFunctor[F[_]] {
def map[A,B](fa: F[A])(f: A => B) : F[B]
}
trait JMapFunctor[K] extends JFunctor[({type LT[V] = Map[K, V]})#LT] {
def map[A, B](fa: Map[K,A])(f: A => B): Map[K,B] = {
@justinhj
justinhj / term.rb
Created December 21, 2018 21:09
vt100 in ruby
# http://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html
# Colours
print "\u001b[30m A \u001b[31m B \u001b[32m C \u001b[33m D \u001b[0m\n"
print "\u001b[34m E \u001b[35m F \u001b[36m G \u001b[37m H \u001b[0m\n"
print "\n"
# Bright colours
@justinhj
justinhj / grahamscan.hs
Created March 18, 2019 15:06
Graham scan exercise from ch3 Real World Haskell
import Data.List
-- Graham Scan
-- Direction data type
data Direction = Left
| Right
| Straight
deriving (Eq, Show)
@justinhj
justinhj / FutureSequenceErrors.scala
Created May 17, 2019 05:54
Example of future sequence
import java.util.concurrent.TimeoutException
import java.util.{Timer, TimerTask}
import scala.concurrent.duration._
import scala.concurrent.{Await, ExecutionContext, Future, Promise}
import scala.language.postfixOps
import scala.util.{Failure, Success, Try}
object FutureUtil {
@justinhj
justinhj / ContravariantAndDivideExample.scala
Created May 21, 2019 16:51
Example of contramap and divide using Scalaz
package examples
import scalaz._, Scalaz._
object ContravariantMap {
// Example of contravariant using Scalaz
trait Printable[A] {
def format(value: A): String
}
@justinhj
justinhj / cats-mtl-1.scala
Created October 4, 2018 19:47
Simple example of cats-mtl library
/*
val CatsMTLVersion = "0.4.0"
val CatsVersion = "1.4.0"
val CatsEffectVersion = "1.0.0"
libraryDependencies ++= Seq(
"org.typelevel" %% "cats-core" % CatsVersion,
"org.typelevel" %% "cats-effect" % CatsEffectVersion,
"org.typelevel" %% "cats-mtl-core" % CatsMTLVersion,
*/
@justinhj
justinhj / MonoidComposition.scala
Created June 12, 2019 16:32
Monoid composition
// Tuples, integers and maps all have monoid
// instances we can easily combine them
@ val data = "Hello Hello This is some data data data yo"
data: String = "Hello Hello This is some data data data yo"
@ def step(word: String) = (1, word.length, Map(word -> 1))
defined function step
@ def run(data: String) = data.split(" ").toList.map(step).combineAll
// Comonad is the dual of Monad
// example for non empty list, extract is the head of the list
// coflatMap gives you successive lists from the whole list, the tail, the tail of that
// and you must return a single value for each list...
@ Comonad[NonEmptyList].extract(NonEmptyList.of(1,2,3))
res26: Int = 1
@ Comonad[NonEmptyList].extract(NonEmptyList.of(1))
res27: Int = 1
@justinhj
justinhj / ActorWithQueue.scala
Created July 4, 2019 12:50
Pub Sub Akka actor with Zio Queue
import zio._
import zio.console._
import zio.{Queue, UIO}
import akka.actor._
import com.typesafe.config.ConfigFactory
import java.util.concurrent.Executors
import scala.concurrent.{ExecutionContext}
import scala.concurrent.duration.FiniteDuration
import java.util.concurrent.{Executors, TimeUnit}
import java.util.concurrent.{ExecutorService, Executors, ThreadFactory}