Skip to content

Instantly share code, notes, and snippets.

View justinhj's full-sized avatar

Justin Heyes-Jones justinhj

View GitHub Profile
@justinhj
justinhj / takeuntiln.scala
Last active April 9, 2018 18:43
Trying to write a function using fs2 to stream until N items have been seen
def takeUntilNThings[F[_],O](n: Long, thing: O): Pipe[F,O,O] = {
def go(s: Stream[F,O], seenCount : Int) : Pull[F,O,Unit] = {
if(seenCount == n) {
Pull.done
}
else {
s.pull.uncons1.flatMap {
@justinhj
justinhj / FS2TakeWhileRepeat.scala
Created April 9, 2018 16:26
FS2 gist to pull from a stream until N items that return true for the provided predicate
def takeWhileRepeat[F[_],O](n: Long, f: O => Boolean): Pipe[F,O,O] = {
def go(s: Stream[F,O], wasTrueCount : Int) : Pull[F,O,Unit] = {
if(wasTrueCount == n) {
Pull.done
}
else {
s.pull.uncons1.flatMap {
@justinhj
justinhj / fs2redissubscriber.scala
Last active April 17, 2018 01:47
Subscribe to a redis channel using an FS2 stream
package com.heyesjones.fs2redis
import cats.effect.{Effect, IO}
import com.redis._
import com.typesafe.scalalogging.LazyLogging
import fs2.{async, _}
import scala.concurrent.ExecutionContext
import scala.util.{Failure, Success, Try}
@justinhj
justinhj / gist:c67f6a239a0670bec79551628d9befba
Created May 2, 2018 21:18 — forked from folone/gist:6089236
Table of unicode operators in scalaz 6.0.x
@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 / 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 / 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 {