Skip to content

Instantly share code, notes, and snippets.

@talesin
talesin / fibn.fs
Last active September 9, 2019 03:01
fib
let fibn n =
let rec loop i a b =
if i >= n then a
else loop (i+1) b (a+b)
loop 0 0 1
@talesin
talesin / DiscriminatedUnionsReflection.fs
Created November 12, 2018 08:28
Example of using reflection with discriminated unions in F#
module DiscriminatedUnionsReflection
open System
open System.ComponentModel
open Xunit
open FSharpPlus
open Microsoft.FSharp.Reflection
type States =
| [<DisplayName("Australian Capital Territory")>] ACT
@talesin
talesin / interleaving.scala
Created June 21, 2017 09:12
Interleaving
import scala.annotation.tailrec
object interleaving {
case class Source[T](name: String, items: Seq[T])
case class Item[T](name: String, value: T) {
override def toString: String = s"$name-$value"
}
val sourcea = Source("A", Seq( 1, 2, 3, 4, 5, 6, 7))
val sourceb = Source("B", Seq( 0, 2, 4, 6, 8,10,12,14,16))
FROM amazonlinux:latest
WORKDIR /app
ENV DOTNET_RUNTIME_URL https://go.microsoft.com/fwlink/?linkid=843405
ENV DOTNET_SDK_URL https://go.microsoft.com/fwlink/?linkid=843449
# Install .NET Core - see https://www.microsoft.com/net/core#linuxcentos
RUN yum install -y libunwind libicu libuuid zip unzip
@talesin
talesin / tree-fold.scala
Last active September 7, 2016 02:25
Attempt at a tail recursive tree fold
def fold[A,B](t: Tree[A], z: B)(f: (B, A) => B)(g: (B,B) => B): B = t match {
case Empty() => z
case Leaf(a) => f(z, a)
case Branch(l: Tree[A], r: Tree[A]) => g(fold(l, z)(f)(g), fold(r, z)(f)(g))
}
def fold2[A,B](t: Tree[A], z: B)(f: (B, A) => B)(g: (B,B) => B): B = {
@annotation.tailrec
def loop(ts: List[(Tree[A], Option[B])], acc: B): B = ts match {
case Nil =>
import shapeless._
object PrintPoly extends Poly1 {
implicit def default[T] = at[(String,T)](_ match {
case (s, Some(x)) => () => println(s"option: $s = $x")
case (s, None) => () => println(s"option: $s = None")
case (s, x) => () => println(s"normal: $s = $x")
})
}
@talesin
talesin / ncrunch-fsproj.xml
Last active October 28, 2015 00:01
NCrunch/fsproj typeprovider reference fix
<Reference Include="FSharp.Data.DesignTime" Condition="$(NCrunch) == '1'">
<HintPath>..\..\packages\FSharp.Data\lib\net40\FSharp.Data.DesignTime.dll</HintPath>
</Reference>
@talesin
talesin / filterAsync.fs
Last active September 13, 2015 06:32
F# Async filter
let filterAsync predicate (items:Async<_> list) = async {
let rec loop acc items' = async {
match items' with
| [] -> return acc
| x :: xs ->
let! item = x
return! loop (if predicate item then item :: acc else acc) xs
}
return! loop [] items
}
@talesin
talesin / json-comparison.fs
Created September 8, 2015 04:22
First pass of tree based subset check go two JSON objects
let rec flatten (token:JToken) = [
if token = null then
()
else if not token.HasValues then
yield (token.Path, token.Type, string token)
yield! flatten token.Next
else
yield! flatten token.First
yield! flatten token.Next
]
param ($Server, $Database, $User, $Password, $Outfile = "$Database.dbml")
set-alias sqlmetal 'C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\SqlMetal.exe'
if ($User -ne $null -and $Password -ne $null) {
sqlmetal /server:$Server /database:$Database /dbml:$Outfile /user:$User /password:$Password
}
else {
sqlmetal /server:$Server /database:$Database /dbml:$Outfile
}