• Stars
    star
    213
  • Rank 179,457 (Top 4 %)
  • Language
    OCaml
  • License
    BSD 3-Clause "New...
  • Created over 6 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A ReasonML/Ocaml library for category theory and abstract algebra

Bastet

npm Build Status Coverage Status Release

A ReasonML/OCaml library for category theory and abstract algebra.

Documentation

See documentation

Installation

Bucklescript/ReasonML

Install the project:

yarn install bs-bastet --save

And add the dependency to your bs-dependencies in bsconfig.json:

"bs-dependencies": [
  "bs-bastet"
]

The project will be available under the Bastet namespace

Native

Install the project:

opam install bastet

Examples

# #require "bastet";;
# open Bastet;;
# module T = Functors.ListF.Option.Traversable;;
module T = Bastet.Functors.ListF.Option.Traversable

# T.sequence [Some "foo"; Some "bar"];;
- : string list option = Some ["foo"; "bar"]

# Functors.ListF.Int.Show.show [1; 1; 2; 3; 5; 8];;
- : string = "[1, 1, 2, 3, 5, 8]"

Suggested Usage

Use kliesli composition

The monadic equivalent of the |> operator is flat_map (>>=). Both are very useful for writing code that's easy to reason about as data all flows in one direction.

However, function composition (>.) and kliesli composition for monads (>=>) are often underused in code. Composing functions and monads monoidally like this in a concatenative style can make a codebase easier to read and can also prevent devs on the team from duplicating code.

Consider a typical use case. Often in a codebase you have something that fetches a value that may or may not exist ('a option). Splitting out this code into smaller functions and combining (and reusing) them with composition leads a lean code style:

# let ((>=>)) = Option.Infix.((>=>));;
val ( >=> ) : ('a -> 'b option) -> ('b -> 'c option) -> 'a -> 'c option =
  <fun>

# type form = { name: string; address: string option };;
type form = { name : string; address : string option; }

# let get_form () =
  (* Assume some side effect got the form here *)
  Some { name = "Foo"; address = Some "123 Bar St." };;
val get_form : unit -> form option = <fun>

# let get_address form = form.address;;
val get_address : form -> string option = <fun>

# let get_form_address = get_form >=> get_address;;
val get_form_address : unit -> string option = <fun>

# get_form_address ();;
- : string option = Some "123 Bar St."

Instantiated Functors

For interfaces based on functors, use already instantiated functors if available to avoid the extra boilerplate:

# Functors.ArrayF.Int.Additive.Fold_Map.fold_map
- : ('a -> int) -> 'a array -> int = <fun>

Don't Overuse Infix

Don't overuse infix operators. If the code is combinatorial it can make it more readable, but in a lot of cases the prefix operators are simpler and easier to read. If you do use infix operators, prefer local opens over global opens to avoid polluting the toplevel:

# let trim_all strings =
  let open List.Infix in
  StringLabels.trim <$> strings;;
val trim_all : string list -> string list = <fun>

# trim_all ["foo "; "bar"; "   baz"];;
- : string list = ["foo"; "bar"; "baz"]

Use Abbreviated Modules

Abbreviated modules can make code both terser and easier to read in some situations, like for example where two different semigroups are used in the same function and infix operators can't be used:

# type game = { score: int; disqualified: bool };;
type game = { score : int; disqualified : bool; }

# let total_score a b =
  let module I = Int.Additive.Semigroup in
  let module B = Bool.Disjunctive.Semigroup in
  { score = I.append a.score b.score; disqualified = B.append a.disqualified b.disqualified };;
val total_score : game -> game -> game = <fun>

# let result =
  let game_1 = { score = 4; disqualified = false }
  and game_2 = { score = 2; disqualified = true }
  in
  total_score game_1 game_2;;
val result : game = {score = 6; disqualified = true}

License

See LICENSE

More Repositories

1

bs-declaredom

Strongly typed declarative markup for the DOM and CSS
OCaml
66
star
2

bs-effects

Bucklescript data structures for effectful sync and async programming
OCaml
37
star
3

callbag-html

An html markup library for callbag
JavaScript
30
star
4

pure-random

✨ A purely functional random number generator
JavaScript
25
star
5

bs-free

Free monads for bucklescript
OCaml
25
star
6

haskell-church-encodings

🌀 Church encodings written in Haskell
Haskell
24
star
7

python-bytecode

A Python bytecode compiler and bytecode generator.
Python
21
star
8

purescript-ffi-utils

A utility library for the purescript foreign function interface
PureScript
21
star
9

lazy-either

🔀 A lazy implementation of the Fantasy Land Either type
JavaScript
16
star
10

do-notation

⬅️ Do notation for Fantasy Land monad types
JavaScript
12
star
11

randomart-js

Generates a randomart image from a buffer
JavaScript
10
star
12

bs-jsverify

Jsverify bindings for bucklescript
OCaml
9
star
13

category-theory-by-example

Explaining category theory by example
PureScript
9
star
14

bs-declaredom-mvc-example

An example of how to use declaredom as an MVC framework in combination with other tools
OCaml
6
star
15

idris-heyting-algebra

Interfaces for heyting algebras and verified bounded join and meet semilattices
Idris
6
star
16

c-autotools

⛵ A tutorial on how to use autotools for C
C
6
star
17

prolog-monty-python

Demo of the Monty Python witch scene with Prolog
Prolog
5
star
18

purescript-most

Most.js bindings for purescript
PureScript
4
star
19

bs-odoc

Simple wrapper around odoc to support bucklescript projects
Shell
4
star
20

haskell-crypto-simple

A simple high level encryption interface based on cryptonite
Haskell
4
star
21

purescript-webcomponents

A web components wrapper for purescript
PureScript
4
star
22

callbag-store

A callbag implementation of a redux-like store
JavaScript
4
star
23

sokoban

🍄 A sokoban client for the browser written in ClojureScript
Clojure
4
star
24

purescript-sqlite

An Sqlite wrapper for Purescript
PureScript
4
star
25

mneme

📚 Mneme is a new and elegant note-taking format with a web editor/viewer.
JavaScript
3
star
26

idris-functors

Bifunctors and functor products, coproducts, and composition
Idris
3
star
27

json-to-dot

Convert a json graph to the dot format
JavaScript
3
star
28

cl-todo-mvc

Todo MVC app for common lisp using ningle + hermetic + parenscript
Common Lisp
3
star
29

purescript-docker

Purescript build for Docker ⚓
Dockerfile
3
star
30

haskell-karatsuba-multiplication

🔡 λ Karatsuba multiplication implemented in Haskell
Haskell
3
star
31

bs-callbag-basics

Tiny and fast reactive/iterable programming library
OCaml
3
star
32

lambda-streams

A lambda-based streaming library (WIP)
OCaml
3
star
33

declaredom

Declarative syntax for generating DOM elements
JavaScript
3
star
34

tabgroups

🍻 A chrome extension that is a variation of the firefox tab group feature.
JavaScript
3
star
35

callbag-element

A custom element that sets its contents from a callbag stream
JavaScript
2
star
36

idris-comonad

An implementation of comonads in idris
Idris
2
star
37

github-markdown-live

:octocat: A simple way to render and view your GitHub markdown files locally
JavaScript
2
star
38

css-parser-combinator

😸 A CSS parser and its combinators
JavaScript
2
star
39

statsd-grapher

📈 A statsd backend that serves an http page with the graphical data
JavaScript
2
star
40

bs-node-stream

Bucklescript bindings for the node stream API
OCaml
2
star
41

bs-tape

Bucklescript bindings for tape
OCaml
2
star
42

c-objects

🚀 My guide on how to implement objects in C with support for inheritance, polymorphism, and clean memory management
C
2
star
43

lambda-streams-promise

Javascript promise helpers for lambda streams
OCaml
2
star
44

idris-naps

A makeshift package management system for idris
JavaScript
2
star
45

asana

A simple yoga asana sequence guide
JavaScript
2
star
46

purescript-starter-template

A purescript starter template using purescript-signal and purescript-smolder-vdom
PureScript
2
star
47

purescript-homogeneous-objects

🈁 Compiler-enforced homogeneous JSON objects for Purescript
PureScript
2
star
48

rxjs-store

A very simple wrapper that provides redux-like functionality using RxJS observables
HTML
1
star
49

bs-callbag-store

A callbag implementation of a redux-like store
OCaml
1
star
50

idris-async

Higher-level abstractions for asynchronous code in idris
Idris
1
star
51

c-video-poker

♣️ A C-implementation of video poker with object-oriented C programming and C-unit testing
C
1
star
52

purescript-properties

A set of generic predicates for testing algebraic properties
PureScript
1
star
53

haskell-quicksort

Quicksort implementation in Haskell
Haskell
1
star
54

purescript-signal-time-travel

A time travel debugger for purescript-signal
PureScript
1
star
55

purescript-pipe-op

Pipe operators (<|) and (|>) for purescript
PureScript
1
star
56

wiktionary-translator

🌍 An open source chrome extension for quick wiktionary translation lookups
HTML
1
star
57

python-church-encodings

Church encodings written in Python
Python
1
star
58

idris-catenable-lists

A port of the purescript catenable-lists library in Idris
Idris
1
star
59

bs-either

DEPRECATED
OCaml
1
star
60

simple-jails

Simple annotated scripts to create FreeBSD thin jails and clone jails
Shell
1
star
61

purescript-lattice

Lattices in purescript
PureScript
1
star
62

bs-dependency-graph

Generates a dependency graph for a bucklescript project
Shell
1
star
63

iac-protocol

📡 A protocol and interface that enables inter-application communication.
Python
1
star
64

purescript-exitcodes

A purescript library for Bash and BSD compliant exit codes
PureScript
1
star
65

tcltk-themes

Tcl Tk themes
Tcl
1
star
66

purescript-undefinable

A library for dealing with undefinable values
PureScript
1
star
67

fiveam-skeleton

A cl-project skeleton that uses fiveam as the test framework
Common Lisp
1
star
68

primary-clipboard

A layer that sets up copy and paste for the PRIMARY clipboard in spacemacs windowless mode
Emacs Lisp
1
star
69

bs-callbag-of

Callbag source factory that emits values specified as arguments.
Shell
1
star