Skip to content

Instantly share code, notes, and snippets.

@pblasucci
Last active October 7, 2022 13:05
Show Gist options
  • Save pblasucci/4ddb37cdec6690a28aab9f65dc84b264 to your computer and use it in GitHub Desktop.
Save pblasucci/4ddb37cdec6690a28aab9f65dc84b264 to your computer and use it in GitHub Desktop.
A contrived example of the "abstract data type" pattern in F#.
(*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <https://unlicense.org>
*)
open System
type MyStack<'T> = { items : 'T list }
// ⮝⮝⮝ If this changes to a discriminated union, nothing outside this file is impacted
[<RequireQualifiedAccess>]
module MyStack =
let empty<'T> = { items = List.empty<'T> }
let isEmpty { items = items } = List.isEmpty items
let push { items = items } item = { items = item :: items }
let pop { items = items } =
match items with
| [] -> None, empty
| h::t -> Some h, { items = t }
type MyStack<'T> with
member me.IsEmpty = MyStack.isEmpty me
member me.Push(item) = item |> MyStack.push me
member me.Pop() = MyStack.pop me
static member Of([<ParamArray>] items : 'T array) =
(MyStack.empty, items) ||> Array.fold (fun s -> s.Push)
(*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <https://unlicense.org>
*)
open System
/// A silly little FILO (first in is last out) structure.
[<Sealed>]
type MyStack<'T> =
/// True when there are no items on this stack.
member IsEmpty : bool
/// Adds the given item to 'top' of this stack.
member Push : item : 'T -> MyStack<'T>
/// If not empty, remove the 'top' item from this stack
/// and return it along with the remainder of this stack.
/// If empty, return 'None' and this stack.
member Pop : unit -> ('T option * MyStack<'T>)
/// Create a new stack from the given items. Items are pushed in array order;
/// so 0th item is at the 'bottom' of the resultant stack.
static member Of : [<ParamArray>] items : 'T array -> MyStack<'T>
[<RequireQualifiedAccess>]
module MyStack =
/// A stack with no items on it.
val empty<'T> : MyStack<'T>
/// True when there are no items on this stack.
val isEmpty : stack : MyStack<'T> -> bool
/// Adds the given item to 'top' of the given stack.
val push : stack : MyStack<'T> -> item : 'T -> MyStack<'T>
/// If not empty, remove the 'top' item from the given stack
/// and return it along with the remainder of the stack.
/// If empty, return 'None' and an empty stack.
val pop : stack : MyStack<'T> -> ('T option * MyStack<'T>)
(*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <https://unlicense.org>
*)
#load "MyStack.fsi" "MyStack.fs"
// comment out ⮝⮝⮝ the signature file and re-check the code-completion hints ;-)
open MyCollections
module Test =
let test () =
let stack1 = 1 |> MyStack.push MyStack.empty
let stack2 = MyStack.Of(1)
stack1 = stack2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment