Skip to content

Instantly share code, notes, and snippets.

@adicirstei
Created October 13, 2016 09:36
Show Gist options
  • Save adicirstei/191946ac1c97490d5af306f607405895 to your computer and use it in GitHub Desktop.
Save adicirstei/191946ac1c97490d5af306f607405895 to your computer and use it in GitHub Desktop.
How do I design this?
/// My function types
type Backward<'Core> = 'Core -> 'Core
type InputCore = {
backward : Backward<InputCore>
x : float
}
type TanhCore = {
backward : Backward<TanhCore>
y : float
}
type Layer =
| Input of InputCore
| Tanh of TanhCore
module Input =
let backward c = // do some core specific computation
{ c with x = c.x * 3.423 }
module Tanh =
let backward c = // do some core specific computation
{ c with x = c.y * c.y }
/// Now, having a list of varoius Layer type instances, I want to call the `backward`
// function on every of them without matching on each.
let backward (lst:Layer list) =
lst
|> List.map (fun l ->
match l with
| Input core -> Input.backward core
| Tanh core -> Tanh.backward core
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment