All Projects → andrejewski → tagmeme

andrejewski / tagmeme

Licence: MIT License
Simple tagged unions

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to tagmeme

Harbol
Harbol is a collection of data structure and miscellaneous libraries, similar in nature to C++'s Boost, STL, and GNOME's GLib
Stars: ✭ 18 (-14.29%)
Mutual labels:  variants
phenomenet-vp
A phenotype-based tool for variant prioritization in WES and WGS data
Stars: ✭ 31 (+47.62%)
Mutual labels:  variants
pathoscore
pathoscore evaluates variant pathogenicity tools and scores.
Stars: ✭ 21 (+0%)
Mutual labels:  variants
pepper
PEPPER-Margin-DeepVariant
Stars: ✭ 179 (+752.38%)
Mutual labels:  variants
needlestack
Multi-sample somatic variant caller
Stars: ✭ 45 (+114.29%)
Mutual labels:  variants
ordo
Ordo: A minimalist language with row polymorphism
Stars: ✭ 50 (+138.1%)
Mutual labels:  variants
recursive-variant
Recursive Variant: A simple library for Recursive Variant Types
Stars: ✭ 67 (+219.05%)
Mutual labels:  variants
TeamTeri
Genomics using open source tools, running on GCP or AWS
Stars: ✭ 30 (+42.86%)
Mutual labels:  variants
VariantRetriever
VariantRetriever is a minimalist package for feature flagging
Stars: ✭ 23 (+9.52%)
Mutual labels:  variants
variantkey
Numerical Encoding for Human Genetic Variants
Stars: ✭ 32 (+52.38%)
Mutual labels:  variants
CuteVCF
simple viewer for variant call format using htslib
Stars: ✭ 30 (+42.86%)
Mutual labels:  variants
vcf stuff
📊Evaluating, filtering, comparing, and visualising VCF
Stars: ✭ 19 (-9.52%)
Mutual labels:  variants
mir-core
Base software building blocks: Algebraic types (aka sumtype/tagged union/variant), universal reflection API, basic math, and more.
Stars: ✭ 23 (+9.52%)
Mutual labels:  tagged-unions
unreachable
Utility function for exhaustiveness checking with typed JS (TS or Flow)
Stars: ✭ 14 (-33.33%)
Mutual labels:  tagged-unions
g3viz
Lollipop-diagram to interactively visualize genetic mutations
Stars: ✭ 19 (-9.52%)
Mutual labels:  variants
pulsar-core
🚀 Handy dynamic styles utilities for React Native and React Native Web.
Stars: ✭ 27 (+28.57%)
Mutual labels:  variants
ftor
ftor enables ML-like type-directed, functional programming with Javascript including reasonable debugging.
Stars: ✭ 44 (+109.52%)
Mutual labels:  tagged-unions
MTBseq source
MTBseq is an automated pipeline for mapping, variant calling and detection of resistance mediating and phylogenetic variants from illumina whole genome sequence data of Mycobacterium tuberculosis complex isolates.
Stars: ✭ 26 (+23.81%)
Mutual labels:  variants
variant
Variant types in TypeScript
Stars: ✭ 147 (+600%)
Mutual labels:  variants
spark-vcf
Spark VCF data source implementation for Dataframes
Stars: ✭ 15 (-28.57%)
Mutual labels:  variants

Tagmeme

Tagged unions

npm install tagmeme

npm Build Status Greenkeeper badge

Tagmeme is a library for building tagged unions. This project offers:

  • A concise API for pattern matching and variant constructors
  • Data-oriented tags that can be serialized and namespaced
  • Errors in development to ensure exhaustive pattern matching
  • Small size and zero-dependencies in production using dead code elimination

Let's check it out.

Examples

import assert from 'assert'
import { union } from 'tagmeme'

const Result = union(['Ok', 'Err'])

const err = Result.Err(new Error('My error'))

const message = Result.match(err, {
  Ok: () => 'No error',
  Err: error => error.message
})

assert(message === 'My error')

const handle = Result.matcher({
  Ok: () => 'No error',
  Err: error => error.message
})

assert(handle(err) === 'My error')

const isError = Result.matches(err, Result.Err)

assert(isError)

Documentation

This package includes:

union

union(types: Array<String>[, options: { prefix: String }]): Union

Create a tagged union. Throws if:

  • types is not an array of unique strings
  • any types are named "match", "matcher", or "matches"

See safeUnion if using arbitrary strings.

Union[type]

Union[type](data: any): ({ type, data })

Create a tag of the union containing data which can be retrieved via Union.match.

import assert from 'assert'
import { union } from 'tagmeme'

const Result = union(['Ok', 'Err'])
const result = Result.Ok('good stuff')

assert(result.type === 'Ok')
assert(result.data === 'good stuff')

Union.match

Union.match(tag, handlers[, catchAll: function])

Pattern match on tag with a hashmap of handlers where keys are kinds and values are functions, with an optional catchAll if no handler matches the value. Throws if:

  • tag is not of any of the union types
  • tag does not match any type and there is no catchAll
  • any handlers key is not a kind in the union
  • any handlers value is not a function
  • it handles all cases and there is a useless catchAll
  • it does not handle all cases and there is no catchAll
import assert from 'assert'
import { union } from 'tagmeme'

const Result = union(['Ok', 'Err'])
const result = Result.Err('Request failed')
const status = Result.match(
  result,
  { Err: () => 400 },
  () => 200
})

assert(status === 400)

Union.matcher

Union.matcher(handlers[, catchAll: function])

Create a matching function which will take tag and context arguments. This reduces the boilerplate of a function that delegates to Union.match with static handlers. This is also a bit faster than match because the handler functions only need to be created once.

Unlike with match, the second argument to handlers will be context to avoid the need for a closure.

import assert from 'assert'
import { union } from 'tagmeme'

const Result = union(['Ok', 'Err'])
const collectErrors = Result.matcher({
  Ok: (_, errors) => errors,
  Err: (error, errors) => errors.concat(error)
})

const errors = collectErrors(Result.Err('Bad'), [])
assert.deepEqual(errors, ['Bad'])

Union.matches

Union.matches(tag, variant: Variant): Boolean

Determine whether a given tag is of variant.

import assert from 'assert'
import { union } from 'tagmeme'

const Result = union(['Ok', 'Err'])
const okTag = Result.Ok(1)

assert(Result.matches(okTag, Result.Ok))

safeUnion

safeUnion(types: Array<String>[, options: { prefix: String }]): { methods, variants }

For library authors accepting arbitrary strings for type names, safeUnion is union but returns distinct collections of methods and type variants. This will not throw if a type is "match", "matcher", or "matches".

Name

tagmeme |ˈtaɡmiːm|: a slot in a syntactic frame which may be filled by any member of a set of appropriate linguistic items.

This name is kind of fitting for a tagged union library.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].