Skip to content

Instantly share code, notes, and snippets.

@battermann
battermann / Music.elm
Created October 27, 2018 21:02
Music Elm
module MusicTheory.Music exposing
( Control(..)
, Division(..)
, Duration(..)
, Music(..)
, Primitive(..)
, PrimitiveGroup(..)
, TiedOrSeparate(..)
, dotted
, eighth
@battermann
battermann / replaceEither.md
Last active October 23, 2018 23:31
Replace Either with Throwing Exceptions

Replace Either with Throwing Exceptions

It is annoying to always unwrap a value from an Either only to be forced to handle the failure case.

Replace the Either value of the failure case with throwing an exception:

// ...
if (numberOfSeats > 1) {
 Right(makeReservation(numberOfSeats))
@battermann
battermann / replaceoptionwithnull.md
Last active October 23, 2018 23:21
Replace Optional with null Refactoring

Replace Option with null refactoring

You are forced to do checks for None and Some and methods like map or flatMap make your code uglier.

Replace the optional value with null:

val customer: Option[Customer] = getCustomer(customerId)

val plan = customer.map(_.plan)
@battermann
battermann / Ports.elm
Created August 4, 2018 10:59
multi-part file upload and Progress
port module Ports exposing (Header, Part, FileUploadRequest, uploadProgress, uploadFile)
import Json.Decode exposing (Value)
port uploadFile : { elementId : ElementId, request : FileUploadRequest } -> Cmd msg
port uploadProgress : (Value -> msg) -> Sub msg
@battermann
battermann / Interval.elm
Created June 17, 2018 19:21
Intervals in Elm
module Types.Interval exposing (IntervalSize(..), IntervalQuality(..), Interval, addIntervalSizeToLetter, noteLetterDistance, addIntervalToNote, perfectUnison, minorSecond, majorSecond, minorThird, majorThird, perfectFourth, augmentedFourth, diminishedFifth, perfectFifth, minorSixth, majorSixth, minorSeventh, majorSeventh)
import Types.Note exposing (..)
import List.Extra
type IntervalSize
= Unison
| Second
| Third
javacOptions ++= Seq("-source", "1.8", "-target", "1.8", "-Xlint", "-g:none")
resolvers += Resolver.sonatypeRepo("releases")
val http4sVersion = "0.18.0"
val circeVersion = "0.9.1"
val catsEffectVersion = "0.9"
libraryDependencies ++= Seq(
"org.http4s" %% "http4s-dsl" % http4sVersion exclude ("org.typelevel", "cats-effect_2.12"),
@battermann
battermann / DiceGame.scala
Last active November 20, 2017 10:13
A simple dice game implemented purely functional.
// Note: pure != good, this is just a demo for the sake of purity. There are no claims that this is a particularly good design.
import PRNG.Seed
import cats.data.{State, StateT}
import cats.effect._
import scala.util.Try
object DiceGame extends App {
type StateIO[A] = StateT[IO, Seed, A]
@battermann
battermann / PRNG.scala
Created October 19, 2017 18:57
functional pseudo random number generation
import cats.data.State
object PRNG {
final case class Seed(seed: Long) {
private lazy val next = Seed(seed * 6364136223846793005L + 1442695040888963407L)
def nextInt: (Seed, Int) = {
(next, (next.seed >>> 16).asInstanceOf[Int])
}
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
package futureeitherapplicativestack
import cats._
import cats.data._
import cats.implicits._
import scala.concurrent.{Await, Future}
import scala.concurrent.duration.Duration
import scala.concurrent.ExecutionContext.Implicits.global