Skip to content

Instantly share code, notes, and snippets.

@Rickasaurus
Rickasaurus / from_into.rs
Created January 24, 2022 17:31
from_into.rs solution
// Steps:
// 1. If the length of the provided string is 0, then return the default of Person
// 2. Split the given string on the commas present in it
// 3. Extract the first element from the split operation and use it as the name
// 4. If the name is empty, then return the default of Person
// 5. Extract the other element from the split operation and parse it into a `usize` as the age
// If while parsing the age, something goes wrong, then return the default of Person
// Otherwise, then return an instantiated Person object with the results
// I AM NOT DONE
type MyDU =
| CaseOne of int
| CaseTwo of int
let c1 = CaseOne 1
let c2 = CaseTwo 2
let matchit mdu =
@Rickasaurus
Rickasaurus / GenericMethodDispatchInBarb.fs
Last active August 29, 2015 14:03
Reflection Over Generic Methods (for invocation in Barb)
module FSharpExpr =
let getMethod =
function
| Patterns.Call (_, m, _) when m.IsGenericMethod -> m.GetGenericMethodDefinition()
| Patterns.Call (_, m, _) -> m
| _ -> failwith "Incorrect getMethod Pattern"
let X<'T> : 'T = Unchecked.defaultof<'T>
let inline application prms expr = Expr.Application(expr, prms)
@Rickasaurus
Rickasaurus / gist:9963727
Last active August 29, 2015 13:58
Folding Enums in F#
type Days =
| None = 0x0
| Sunday = 0x1
| Monday = 0x2
| Tuesday = 0x4
| Wednesday = 0x8
| Thursday = 0x10
| Friday = 0x20
| Saturday = 0x40
@Rickasaurus
Rickasaurus / nested lets and let in
Last active August 29, 2015 13:57
Fun with let
let y =
let x = 10
x * 10
let timesTen y =
let x = 10 in y * x
@Rickasaurus
Rickasaurus / jsonsample.fsx
Created October 8, 2013 21:12
pseudo-dynamic json parsing into a DU with FSharp.Data
type ParseError = string list
type HashBlocking = { CustomerField: string; ProviderField: string }
type SuffixArrayBlocking = { CustomerField: string; ProviderField: string }
type BlockingMethod =
| Hash of HashBlocking
| SuffixArray of SuffixArrayBlocking
type FieldConfig =
@Rickasaurus
Rickasaurus / json
Created October 8, 2013 21:11
Sample psudo-dynamic json parsing with a DU via FSharp.Data
type ParseError = string list
type HashBlocking = { CustomerField: string; ProviderField: string }
type SuffixArrayBlocking = { CustomerField: string; ProviderField: string }
type BlockingMethod =
| Hash of HashBlocking
| SuffixArray of SuffixArrayBlocking
type FieldConfig =
@Rickasaurus
Rickasaurus / gist:6891666
Created October 8, 2013 21:05
Parsing semi-dynamic JSON with FSharp.Data
type ParseError = string list
type HashBlocking = { CustomerField: string; ProviderField: string }
type SuffixArrayBlocking = { CustomerField: string; ProviderField: string }
type BlockingMethod =
| Hash of HashBlocking
| SuffixArray of SuffixArrayBlocking
type FieldConfig =