Skip to content

Instantly share code, notes, and snippets.

@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.
//
@battermann
battermann / Siren.fs
Last active January 27, 2017 16:49
An F# representation of the Siren media type https://github.com/kevinswiber/siren
module Siren
open System
open Hypermedia.Models
type Rel = Rel of string
type Title = Title of string
type Class = Class of string
type MediaType = MediaType of string
@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 / free.fsx
Last active February 17, 2022 23:26
Free Monad like pattern in F#
#load @"paket-files/fsprojects/Chessie/src/Chessie/ErrorHandling.fs"
type Continuation<'output, 'next> = 'output -> 'next
module TerminalDsl =
open Chessie.ErrorHandling
type Terminal<'next> =
| WriteLine of string * Continuation<unit, 'next>
| ReadLine of unit * Continuation<string, 'next>
@battermann
battermann / io.fsx
Last active July 6, 2020 15:21
IO Monad in F#
[<AutoOpen>]
module IO =
type IO<'a> =
private
| Return of (unit -> 'a)
| Suspend of (unit -> IO<'a>)
let rec run x =
match x with
| Return v -> v()
#load @"paket-files/fsprojects/Chessie/src/Chessie/ErrorHandling.fs"
open System
open Chessie.ErrorHandling
type Person = { FirstName: string; LastName: string }
let id1 = Guid.NewGuid()
let id2 = Guid.NewGuid()
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
public class EditedPair<T>
{
public EditedPair(T old, T @new)
{
Old = old;
New = @new;
}
public T Old { get; private set; }
public T New { get; private set; }