aboutsummaryrefslogtreecommitdiff
path: root/format.go
blob: d635be967cd533866c6ca427ad10d12e1496b82b (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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main

import (
	"fmt"
	"math"
)

type S string       // string
type I int64        // int
type F float64      // float
type C complex128   // complex
type A interface{}  // any
type M func(A) A    // monad
type D func(A, A) A // dyad

func (s S) String() string {
	return string(s)
}

func (i I) String() string {
	pre := ""
	if i < 0 {
		pre = "¯"
		i = -i
	}
	return fmt.Sprint(pre, int64(i))
}

func (f F) String() string {
	pre := ""
	if f < 0 {
		pre = "¯"
		f = -f
	}
	return fmt.Sprint(pre, float64(f))
}

func (c C) String() string {
	return fmt.Sprint(F(real(c)), "J", F(imag(c)))
}

func IsInt(c complex128) bool {
	return imag(c) == 0 && math.Trunc(real(c)) == real(c)
}

func IsFloat(c complex128) bool {
	return imag(c) == 0 && math.Trunc(real(c)) != real(c)
}

func IsComplex(c complex128) bool {
	return imag(c) != 0
}