aboutsummaryrefslogtreecommitdiff
path: root/monad.go
blob: 083a3de731c28ab75a141c7ff55b1f3cf9952792 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package main

import "math/cmplx"

var monads = map[rune]Monad{
	'+': identity,
	'-': negate,
	'×': direction,
	'÷': reciprocal,
}

func identity(d complex128) complex128   { return cmplx.Conj(d) }
func negate(d complex128) complex128     { return -d }
func direction(d complex128) complex128  { return d / complex(cmplx.Abs(d), 0) }
func reciprocal(d complex128) complex128 { return 1.0 / d }

var dyads = map[rune]Dyad{
	'+': plus,
	'-': minus,
	'×': times,
	'÷': divide,
}

func plus(a, w complex128) complex128   { return a + w }
func minus(a, w complex128) complex128  { return a - w }
func times(a, w complex128) complex128  { return a * w }
func divide(a, w complex128) complex128 { return a / w }