├── .gitignore ├── modules ├── binsrch │ ├── go │ │ ├── .gitignore │ │ ├── binsrch │ │ │ ├── .gitignore │ │ │ ├── go.mod │ │ │ └── binsrch_test.go │ │ ├── go.mod │ │ ├── gen │ │ │ ├── go.mod │ │ │ └── gen.go │ │ ├── test.bash │ │ ├── binsrch.tmpl │ │ └── binsrch_test.go │ └── README.md ├── esieve │ ├── go.mod │ ├── test.bash │ ├── esieve.go │ ├── esieve_test.go │ └── README.md ├── lstrev │ ├── go.mod │ ├── lstrev.go │ ├── README.md │ └── lstrev_test.go ├── lrucache │ ├── go.mod │ ├── lrucache_test.go │ ├── lrucache.go │ └── README.md ├── memo │ ├── go.mod │ ├── memo.go │ └── memo_test.go ├── mndlbrot │ ├── go.mod │ └── main.go └── fibseq │ ├── go │ ├── go.mod │ ├── fibseq_test.go │ └── fibseq.go │ └── README.md ├── README.md └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode/ 2 | -------------------------------------------------------------------------------- /modules/binsrch/go/.gitignore: -------------------------------------------------------------------------------- 1 | /binsrch.go 2 | -------------------------------------------------------------------------------- /modules/binsrch/go/binsrch/.gitignore: -------------------------------------------------------------------------------- 1 | /binsrch.go 2 | -------------------------------------------------------------------------------- /modules/esieve/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/skurhse/gresto/esieve 2 | 3 | go 1.21 4 | -------------------------------------------------------------------------------- /modules/lstrev/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/skurhse/gresto/lstrev 2 | 3 | go 1.21 4 | -------------------------------------------------------------------------------- /modules/lrucache/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/skurhse/gresto/lrucache 2 | 3 | go 1.21 4 | -------------------------------------------------------------------------------- /modules/memo/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/skurhse/fisher/modules/memo 2 | 3 | go 1.22.4 4 | -------------------------------------------------------------------------------- /modules/mndlbrot/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/skurhse/gresto/mndlbrot 2 | 3 | go 1.21.4 4 | -------------------------------------------------------------------------------- /modules/fibseq/go/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/skurhse/fisher/modules/fibseq 2 | 3 | go 1.22 4 | -------------------------------------------------------------------------------- /modules/binsrch/go/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/skurhse/fisher/modules/binsrch 2 | 3 | go 1.22.3 4 | -------------------------------------------------------------------------------- /modules/binsrch/go/gen/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/skurhse/fisher/modules/binsrch/go 2 | 3 | go 1.22.3 4 | -------------------------------------------------------------------------------- /modules/binsrch/go/binsrch/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/skurhse/fisher/modules/binsrch/go/binsrch 2 | 3 | go 1.22.3 4 | -------------------------------------------------------------------------------- /modules/esieve/test.bash: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # REQ: Runs go tests and benchmarks. 4 | 5 | set -Cefuvxo pipefail 6 | 7 | dir=$(dirname "$0") 8 | cd "$dir" 9 | 10 | go test -v -bench=. -benchmem 11 | -------------------------------------------------------------------------------- /modules/binsrch/go/test.bash: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -o errexit 4 | set -o nounset 5 | set -o xtrace 6 | 7 | path=$(realpath "$BASH_SOURCE") 8 | 9 | dir=$(dirname "$path") 10 | 11 | cd "$dir" 12 | 13 | pushd "$dir/gen"; go generate; popd 14 | 15 | go test -bench=. 16 | -------------------------------------------------------------------------------- /modules/fibseq/go/fibseq_test.go: -------------------------------------------------------------------------------- 1 | package fibseq 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func ExampleNumber_one() { 8 | num := Number(2) 9 | fmt.Println(num) 10 | // Output: 11 | // 1 12 | } 13 | 14 | func ExampleNumber_two() { 15 | num := Number(3) 16 | fmt.Println(num) 17 | // Output: 18 | // 2 19 | } 20 | 21 | func ExampleNumber_three() { 22 | num := Number(4) 23 | fmt.Println(num) 24 | // Output: 25 | // 3 26 | } 27 | -------------------------------------------------------------------------------- /modules/fibseq/go/fibseq.go: -------------------------------------------------------------------------------- 1 | package fibseq 2 | 3 | const ( 4 | First = 0 5 | Second = 1 6 | ) 7 | 8 | func Number(n int) int { 9 | switch n { 10 | case 0: 11 | return First 12 | case 1: 13 | return Second 14 | } 15 | 16 | seq := make([]int, 0, n) 17 | seq = append(seq, First, Second) 18 | n -= 2 19 | 20 | return number(n, seq) 21 | } 22 | 23 | func number(n int, seq []int) int { 24 | l := len(seq) 25 | next := seq[l-1] + seq[l-2] 26 | 27 | if n == 0 { 28 | return next 29 | } 30 | 31 | seq = append(seq, next) 32 | n-- 33 | return number(n, seq) 34 | } 35 | -------------------------------------------------------------------------------- /modules/esieve/esieve.go: -------------------------------------------------------------------------------- 1 | package eseive 2 | 3 | func MakePrimes(lower int, upper int) []int { 4 | if upper < 2 { 5 | return []int{} 6 | } 7 | if lower < 2 { 8 | lower = 2 9 | } 10 | 11 | primes := []int{} 12 | composites := make(map[int]bool, lower-upper) 13 | 14 | for i := 2; i*i < upper; i++ { 15 | if composites[i] { 16 | continue 17 | } 18 | for m := i + i; m < upper; m += i { 19 | if m >= lower { 20 | composites[m] = true 21 | } 22 | } 23 | } 24 | for i := lower; i < upper; i++ { 25 | if !composites[i] { 26 | primes = append(primes, i) 27 | } 28 | } 29 | 30 | return primes 31 | } 32 | -------------------------------------------------------------------------------- /modules/esieve/esieve_test.go: -------------------------------------------------------------------------------- 1 | package eseive 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func ExampleMakePrimes_one() { 9 | primes := MakePrimes(0, 100) 10 | fmt.Println(primes) 11 | // Output: [2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97] 12 | } 13 | 14 | func ExampleMakePrimes_two() { 15 | primes := MakePrimes(10000, 10100) 16 | fmt.Println(primes) 17 | // Output: [10007 10009 10037 10039 10061 10067 10069 10079 10091 10093 10099] 18 | } 19 | 20 | func ExampleMakePrimes_three() { 21 | primes := MakePrimes(1000000, 1000100) 22 | fmt.Println(primes) 23 | // Output: [1000003 1000033 1000037 1000039 1000081 1000099] 24 | } 25 | 26 | func BenchmarkMakePrimes(b *testing.B) { 27 | for i := 0; i < b.N; i++ { 28 | MakePrimes(2, i) 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /modules/lrucache/lrucache_test.go: -------------------------------------------------------------------------------- 1 | package lrucache 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | "strings" 7 | ) 8 | 9 | func ExampleLRUCacheOne() { 10 | 11 | actions := []string{"LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"} 12 | values := [][]int{{2}, {1, 1}, {2, 2}, {1}, {3, 3}, {2}, {4, 4}, {1}, {3}, {4}} 13 | 14 | ExemplifyLRUCache(actions, values) 15 | // Output: 16 | //[null, null, null, 1, null, -1, null, -1, 3, 4] 17 | } 18 | 19 | func ExemplifyLRUCache(actions []string, values [][]int) { 20 | var lruCache *LRUCache 21 | output := make([]any, len(actions)) 22 | for i, action := range actions { 23 | switch action { 24 | case "LRUCache": 25 | lruCache = New(values[i][0]) 26 | case "put": 27 | lruCache.Put(values[i][0], values[i][1]) 28 | case "get": 29 | output[i] = lruCache.Get(values[i][0]) 30 | } 31 | } 32 | 33 | json, err := json.Marshal(output) 34 | if err != nil { 35 | panic(err) 36 | } 37 | 38 | fmt.Println(strings.ReplaceAll(string(json), ",", ", ")) 39 | } 40 | -------------------------------------------------------------------------------- /modules/lstrev/lstrev.go: -------------------------------------------------------------------------------- 1 | package lstrev 2 | 3 | type ListNode struct { 4 | Value int 5 | Next *ListNode 6 | } 7 | 8 | func ReverseListRec(head *ListNode) *ListNode { 9 | return reverse(nil, head) 10 | } 11 | 12 | func reverse(prev *ListNode, curr *ListNode) *ListNode { 13 | if curr == nil { 14 | return prev 15 | } 16 | 17 | next := curr.Next 18 | curr.Next = prev 19 | 20 | return reverse(curr, next) 21 | } 22 | 23 | func ReverseListItr(head *ListNode) *ListNode { 24 | var prev, next *ListNode 25 | for curr := head; curr != nil; prev, curr = curr, next { 26 | next = curr.Next 27 | curr.Next = prev 28 | } 29 | 30 | return prev 31 | } 32 | 33 | func NewList(s []int) *ListNode { 34 | if len(s) == 0 { 35 | return nil 36 | } 37 | 38 | h := &ListNode{s[0], nil} 39 | n := h 40 | for _, v := range s[1:] { 41 | n.Next = &ListNode{v, nil} 42 | n = n.Next 43 | } 44 | return h 45 | } 46 | 47 | func (h *ListNode) ToSlice() []int { 48 | if h == nil { 49 | return []int{} 50 | } 51 | 52 | s := make([]int, 0) 53 | for n := h; n != nil; n = n.Next { 54 | s = append(s, n.Value) 55 | } 56 | return s 57 | } 58 | -------------------------------------------------------------------------------- /modules/lstrev/README.md: -------------------------------------------------------------------------------- 1 | # Reverse Linked List 2 | 3 | In computer science, a linked list is a linear collection of data elements whose order, unlike an array, is not inherent to their placement in memory. Instead, each element points to the next, and together this collection of nodes represents a sequence. 4 | 5 | In a *singly* linked list, each node contains a data value and a 'next' reference. This structure allows for removal of (or insertions around) accessed elements in constant or O(1) time. A drawback of linked lists however, is that raw access requires iteration, which is done in linear or O(N) time. 6 | 7 | **Challenge** 8 | 9 | Consider the following class definition: 10 | ```python 11 | # Definition for singly-linked list. 12 | class ListNode: 13 | def __init__(self, val=0, next=None): 14 | self.val = val 15 | self.next = next 16 | ``` 17 | Given the head of a singly linked list, return the reversed list. 18 | 19 | Example One: 20 | ``` 21 | Input: head = [1,2,3,4,5] 22 | Output: [5,4,3,2,1] 23 | ``` 24 | Example Two: 25 | ``` 26 | Input: head = [1] 27 | Output: [1] 28 | ``` 29 | Example Three: 30 | ``` 31 | Input: head = [] 32 | Output: [] 33 | ``` 34 | 35 | Both recursive and iterative solutions exist. Which one is faster? 36 | -------------------------------------------------------------------------------- /modules/lrucache/lrucache.go: -------------------------------------------------------------------------------- 1 | package lrucache 2 | 3 | import ( 4 | "container/list" 5 | ) 6 | 7 | type LRUCache struct { 8 | capacity int 9 | store map[int]*list.Element 10 | usage *list.List 11 | } 12 | 13 | type LRUEntry struct { 14 | key int 15 | value int 16 | } 17 | 18 | func New(capacity int) *LRUCache { 19 | return &LRUCache{ 20 | capacity: capacity, 21 | store: make(map[int]*list.Element, capacity), 22 | usage: list.New(), 23 | } 24 | } 25 | 26 | func (cache *LRUCache) Get(key int) int { 27 | element, ok := cache.store[key] 28 | 29 | if ok { 30 | cache.usage.MoveToFront(element) 31 | return element.Value.(*LRUEntry).value 32 | } 33 | 34 | return -1 35 | } 36 | 37 | func (cache *LRUCache) Put(key int, value int) { 38 | element, ok := cache.store[key] 39 | 40 | if ok { 41 | cache.usage.MoveToFront(element) 42 | element.Value.(*LRUEntry).value = value 43 | return 44 | } 45 | 46 | if cache.usage.Len() == cache.capacity { 47 | backElement := cache.usage.Back() 48 | cache.usage.Remove(backElement) 49 | 50 | backKey := backElement.Value.(*LRUEntry).key 51 | delete(cache.store, backKey) 52 | 53 | } 54 | 55 | entry := &LRUEntry{key: key, value: value} 56 | 57 | element = cache.usage.PushFront(entry) 58 | 59 | cache.store[key] = element 60 | return 61 | } 62 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 🎣 fisher 2 | - a go [chrestomathy](https://en.wiktionary.org/wiki/chrestomathy) for [1.24](https://go.dev/dl/) 3 | - a rust [miscellanea](https://en.wiktionary.org/wiki/miscellanea) for [1.88](https://forge.rust-lang.org/infra/other-installation-methods.html#standalone-installer) 4 | 5 | ### Table of Contents 6 | 7 | - [Binary Search on Common Attributes](modules/binsrch/) 8 | - [Sieve of Eratosthenes](modules/esieve/) 9 | - [Fibonacci Sequence](modules/fibseq/) 10 | - [Least Recently Used (LRU) Cache Replacement](modules/lrucache/) 11 | - [Concurrency-safe Memoizer](modules/memo) 12 | - [Reverse Linked List](modules/lstrev/) 13 | - [Mandelbrot Set](modules/mndlbrot/) 14 | 15 | ### Addendum 16 | 17 | #### Official Resources 18 | - [Documentation](https://go.dev/doc/) 19 | - [Source Code](https://github.com/golang/go#the-go-programming-language) 20 | - [Blog](https://go.dev/blog/) 21 | - [A Tour of Go](https://go.dev/tour/list) 22 | - [How to Write Go Code](https://go.dev/doc/code) 23 | - [Effective Go](https://go.dev/doc/effective_go) 24 | 25 | #### Community Resources 26 | - [Go Proverbs](https://go-proverbs.github.io/) 27 | - [Go by Example](https://gobyexample.com/) 28 | - [Go Track (Exercism)](https://exercism.org/tracks/go) 29 | - [Go First Steps (Microsoft Training Path)](https://learn.microsoft.com/en-us/training/paths/go-first-steps/) 30 | -------------------------------------------------------------------------------- /modules/binsrch/README.md: -------------------------------------------------------------------------------- 1 | # Binary Search on Common Attributes 2 | 3 | Also known as logarithmic search, *binary search* is an algorithm that finds the index of a target value within a sorted collection. 4 | 5 | Binary search leverages the ordinal nature of sorted candidate data by repeatedly bifurcating the search interval until the target is found or the interval becomes empty (no result). 6 | 7 | Although the basic idea behind the algorithm is straightforward, the details can be tricky: 8 | - Variables used to represent indices can result in an arithmetic overflow against large collections. 9 | - If elements are repeated, two modified binary searches must be completed in order to capture the lower and upper bounds. 10 | - A recursive solution will be slower than an iterative solution unless tail-call optimization is implemented correctly. 11 | 12 | ### Problem Statement 13 | 14 | Write two functions to perform binary searches against the abscissa (x-coordinate) in a sorted collection of cartesian points: 15 | ```go 16 | type Point struct { 17 | X int 18 | Y int 19 | } 20 | 21 | func Search(points []Point, abscissa int) (int, int, error) {} 22 | ``` 23 | 24 | Return the half-open interval of the found subrange. 25 | 26 | ##### Notes 27 | - Coordinates may be either natural or negative whole numbers. 28 | - Ordered pairs with x-coordinate values. 29 | -------------------------------------------------------------------------------- /modules/esieve/README.md: -------------------------------------------------------------------------------- 1 | # Sieve of Eratosthenes 2 | 3 | *Sift the Two's and Sift the Three's* 4 | 5 | *The Sieve of Eratosthenes* 6 | 7 | *When the multiples sublime* 8 | 9 | *The numbers that remain are Prime* 10 | 11 | ### Problem Description 12 | 13 | In mathematics, the sieve of Eratosthenes is an ancient algorithm for finding prime numbers. Its invention is attributed to Eratosthenes of Cyrene, a 3rd century BCE Greek mathematician. 14 | 15 | The algorithm works to calculate primes by iteratively marking as composite (i.e., not prime) the multiples of each prime, starting with the first prime number, When the next identified prime exceeds the square root of the upper limit, all the remaining numbers in the list are prime. 16 | 17 | *Euler's sieve* is a version of the sieve of Eratosthenes in which each composite number is eliminated exactly once. It, too, starts with a list of numbers from 2 to n in order. 18 | 19 | On each step the first element is identified as the next prime. This prime is multiplied with each element of the list, including itself, and the results are marked in the list for subsequent deletion. The initial element and the marked elements are then removed from the working sequence, and the process is repeated. When the next identified prime exceeds the square root of the upper limit, all the remaining numbers in the list are prime. 20 | -------------------------------------------------------------------------------- /modules/binsrch/go/gen/gen.go: -------------------------------------------------------------------------------- 1 | //go:generate go run generate.go 2 | 3 | package main 4 | 5 | import ( 6 | "log" 7 | "os" 8 | "text/template" 9 | ) 10 | 11 | type Bound struct { 12 | Name string 13 | IsLower bool 14 | IsUpper bool 15 | Index int 16 | Polarity int 17 | Shift int 18 | } 19 | 20 | type Coord struct { 21 | Name string 22 | } 23 | 24 | type Printer struct { 25 | Lower Bound 26 | Upper Bound 27 | Bounds [2]Bound 28 | Coords [2]Coord 29 | } 30 | 31 | var ( 32 | coords = [2]Coord{ 33 | { 34 | Name: "X", 35 | }, 36 | { 37 | Name: "Y", 38 | }, 39 | } 40 | 41 | lower = Bound{ 42 | Name: "Lower", 43 | IsLower: true, 44 | IsUpper: false, 45 | Index: -1, 46 | Polarity: -1, 47 | Shift: 0, 48 | } 49 | 50 | upper = Bound{ 51 | Name: "Upper", 52 | IsLower: false, 53 | IsUpper: true, 54 | Index: -1, 55 | Polarity: 1, 56 | Shift: 1, 57 | } 58 | 59 | printer = Printer{ 60 | Lower: lower, 61 | Upper: upper, 62 | Bounds: [2]Bound{lower, upper}, 63 | Coords: coords, 64 | } 65 | ) 66 | 67 | func main() { 68 | template := template.Must(template.ParseFiles("../binsrch.tmpl")) 69 | 70 | out, err := os.Create("../binsrch.go") 71 | 72 | if err != nil { 73 | log.Fatalf("%v", err) 74 | } 75 | defer out.Close() 76 | 77 | err = template.Execute(out, printer) 78 | 79 | if err != nil { 80 | log.Fatalf("%v", err) 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /modules/lstrev/lstrev_test.go: -------------------------------------------------------------------------------- 1 | package lstrev_test 2 | 3 | import ( 4 | "fmt" 5 | "math/rand" 6 | "reflect" 7 | "runtime" 8 | "testing" 9 | "time" 10 | 11 | . "github.com/skurhse/gresto/lstrev" 12 | ) 13 | 14 | type assert struct { 15 | input []int 16 | want []int 17 | } 18 | 19 | var funcs []func(*ListNode) *ListNode = []func(*ListNode) *ListNode{ReverseListRec, ReverseListItr} 20 | 21 | func TestReverseLists(t *testing.T) { 22 | var data = []assert{ 23 | {[]int{1, 2, 3}, []int{3, 2, 1}}, 24 | {[]int{1}, []int{1}}, 25 | {[]int{}, []int{}}, 26 | } 27 | 28 | for _, dtm := range data { 29 | for _, fnc := range funcs { 30 | input, want := NewList(dtm.input), NewList(dtm.want) 31 | 32 | fname := runtime.FuncForPC(reflect.ValueOf(fnc).Pointer()).Name() 33 | testname := fmt.Sprintf("%s(%v)", fname, input.ToSlice()) 34 | 35 | t.Run(testname, func(t *testing.T) { 36 | if got := fnc(input); !reflect.DeepEqual(got, want) { 37 | t.Errorf("got %v, want %v", got.ToSlice(), want.ToSlice()) 38 | } 39 | }) 40 | } 41 | } 42 | } 43 | 44 | func BenchmarkReverseLists(b *testing.B) { 45 | rand.Seed(time.Now().Unix()) 46 | list := NewList(rand.Perm(1000000)) 47 | 48 | for _, fnc := range funcs { 49 | fname := runtime.FuncForPC(reflect.ValueOf(fnc).Pointer()).Name() 50 | 51 | b.Run(fname, func(b *testing.B) { 52 | for i := 0; i < b.N; i++ { 53 | fnc(list) 54 | } 55 | }) 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /modules/memo/memo.go: -------------------------------------------------------------------------------- 1 | package memo 2 | 3 | // Package memo provides a `Memoizer` server 4 | // for concurrency-safe caching of `Calculator` results. 5 | 6 | type Memoizer struct { 7 | requests chan request 8 | } 9 | 10 | type Calculator func(key string) (interface{}, error) 11 | 12 | type request struct { 13 | key string 14 | result chan<- result 15 | } 16 | 17 | type result struct { 18 | val interface{} 19 | err error 20 | } 21 | 22 | type entry struct { 23 | result result 24 | ready chan struct{} 25 | } 26 | 27 | func New(calc Calculator) (memo *Memoizer) { 28 | memo = &Memoizer{ 29 | requests: make(chan request), 30 | } 31 | 32 | go memo.serve(calc) 33 | return 34 | } 35 | 36 | func (memo *Memoizer) Get(key string) (interface{}, error) { 37 | ch := make(chan result) 38 | 39 | memo.requests <- request{key, ch} 40 | res := <-ch 41 | 42 | return res.val, res.err 43 | } 44 | 45 | func (memo *Memoizer) Close() { 46 | close(memo.requests) 47 | } 48 | 49 | func (memo *Memoizer) serve(calc Calculator) { 50 | cache := make(map[string]*entry) 51 | 52 | for req := range memo.requests { 53 | e := cache[req.key] 54 | 55 | if e == nil { 56 | e = &entry{ready: make(chan struct{})} 57 | cache[req.key] = e 58 | 59 | go e.call(calc, req.key) 60 | } 61 | 62 | go e.deliver(req.result) 63 | } 64 | } 65 | 66 | func (e *entry) call(calc Calculator, key string) { 67 | e.result.val, e.result.err = calc(key) 68 | close(e.ready) 69 | } 70 | 71 | func (e *entry) deliver(res chan<- result) { 72 | <-e.ready 73 | res <- e.result 74 | } 75 | -------------------------------------------------------------------------------- /modules/fibseq/README.md: -------------------------------------------------------------------------------- 1 | # Fibonacci Sequence 2 | 3 | In mathematics, the "Fibonacci sequence" is a numeric sequence in which each element is the sum of the two preceding. 4 | 5 | ![Tiled squares representing the Fibonacci sequence](https://github.com/skurhse/fisher/assets/8763488/b216bb2e-2b44-4d95-9190-603590a10b6a) 6 | 7 | The sequence commonly starts from 0 and 1, although some authors start the sequence from 1 and 1, or from 1 and 2, as did Fibonacci. 8 | 9 | The Fibonacci numbers were first described in Indian mathematics as early as 200 BC in work by poet and mathmetician [Acharya Pingala](https://en.wikipedia.org/wiki/Pingala) on enumerating patterns of Sanskrit poetry, in a sequence called *mātrāmeru*. 10 | 11 | ![Pingala](https://github.com/skurhse/fisher/assets/8763488/165a72ce-aaba-4503-9cfb-9315ed49cdf4) 12 | 13 | Pingala was a scholar in the Maurya Empire, a geographically extensive Iron Age historical power in South Asia. Pingala is sometimes accredited with the first use of the number zero, as he used the Sanskrit word *śūnya* to explicitly refer to the number. 14 | 15 | The Fibonacci numbers are named after the Italian mathematician Leonardo of Pisa, also known as [Fibonacci](https://en.wikipedia.org/wiki/Fibonacci). 16 | 17 | Leonardo introduced the sequence to Western European mathematics in his 1202 book [Liber Abaci](https://en.wikipedia.org/wiki/Liber_Abaci). 18 | 19 | Fibonacci travelled around the Mediterranean coast, meeting with merchants and learning about their systems of doing arithmetic. 20 | 21 | ![50S -Leonardo-Fibonacci-1170-1250-594x594](https://github.com/skurhse/fisher/assets/8763488/52a92d9d-a11d-4eee-ad06-8e65aa48a6d8) 22 | -------------------------------------------------------------------------------- /modules/binsrch/go/binsrch.tmpl: -------------------------------------------------------------------------------- 1 | package binsrch 2 | 3 | type Coordinate interface { 4 | ~int | ~int8 | ~int32 | ~int64 5 | } 6 | 7 | type Point[C Coordinate] struct { 8 | {{range .Coords}} 9 | {{.Name}} C 10 | {{end}} 11 | } 12 | 13 | type Points[C Coordinate] []Point[C] 14 | 15 | {{range .Coords}} 16 | {{ $coord := . }} 17 | func (points Points[C]) Where{{.Name}}(target C) (Points[C], bool) { 18 | size := len(points) 19 | 20 | if size == 0 { 21 | return nil, false 22 | } 23 | 24 | var lower int 25 | var upper int 26 | {{range $.Bounds}} 27 | lower = 0 28 | upper = size - 1 29 | var val{{.Name}} int 30 | for { 31 | windowSize := (upper - lower) + 1 32 | 33 | if windowSize == 1 { 34 | {{if .IsLower}} 35 | last := points[lower].{{$coord.Name}} 36 | 37 | if last == target { 38 | val{{.Name}} = lower 39 | break 40 | } 41 | 42 | val{{.Name}} = -1 43 | {{else}} 44 | val{{.Name}} = lower 45 | {{end}} 46 | break 47 | } 48 | 49 | jump := lower + windowSize/2 50 | 51 | test := points[jump].{{$coord.Name}} 52 | 53 | if test == target { 54 | look := jump + {{.Polarity}} 55 | 56 | if look < 0 || look == len(points) { 57 | val{{.Name}} = jump 58 | break 59 | } 60 | 61 | if points[look].{{$coord.Name}} != target { 62 | val{{.Name}} = jump 63 | break 64 | } 65 | 66 | {{if .IsLower}} 67 | upper = jump + {{.Polarity}} 68 | {{else}} 69 | lower = jump + {{.Polarity}} 70 | {{end}} 71 | } 72 | 73 | if test > target { 74 | upper = jump - 1 75 | } else { 76 | lower = jump + 1 77 | } 78 | } 79 | 80 | {{if .IsLower}} 81 | if val{{.Name}} == -1 { 82 | return nil, false 83 | } 84 | {{end}} 85 | {{end}} 86 | 87 | return points[val{{$.Lower.Name}} : val{{$.Upper.Name}} + 1], true 88 | } 89 | {{end}} 90 | -------------------------------------------------------------------------------- /modules/memo/memo_test.go: -------------------------------------------------------------------------------- 1 | package memo 2 | 3 | import ( 4 | "fmt" 5 | "io/ioutil" 6 | "log" 7 | "net/http" 8 | "sync" 9 | "testing" 10 | "time" 11 | ) 12 | 13 | func TestSequential(t *testing.T) { 14 | memo := New(httpGetBody) 15 | defer memo.Close() 16 | 17 | testSequential(t, memo) 18 | } 19 | 20 | func TestConcurrent(t *testing.T) { 21 | memo := New(httpGetBody) 22 | defer memo.Close() 23 | 24 | testConcurrent(t, memo) 25 | } 26 | 27 | func httpGetBody(url string) (interface{}, error) { 28 | resp, err := http.Get(url) 29 | if err != nil { 30 | return nil, err 31 | } 32 | defer resp.Body.Close() 33 | 34 | return ioutil.ReadAll(resp.Body) 35 | } 36 | 37 | func httpGetURLs() <-chan string { 38 | ch := make(chan string) 39 | go func() { 40 | for _, url := range []string{ 41 | "https://golang.org", 42 | "https://godoc.org", 43 | "https://play.golang.org", 44 | "https://go.dev", 45 | "https://golang.org", 46 | "https://godoc.org", 47 | "https://play.golang.org", 48 | "https://go.dev", 49 | } { 50 | ch <- url 51 | } 52 | close(ch) 53 | }() 54 | 55 | return ch 56 | } 57 | 58 | type M interface { 59 | Get(url string) (interface{}, error) 60 | } 61 | 62 | func testSequential(t *testing.T, m M) { 63 | for url := range httpGetURLs() { 64 | 65 | val, err := m.Get(url) 66 | 67 | if err != nil { 68 | log.Print(err) 69 | continue 70 | } 71 | fmt.Printf("%s %d bytes\n", url, len(val.([]byte))) 72 | } 73 | } 74 | 75 | func testConcurrent(t *testing.T, m M) { 76 | var n sync.WaitGroup 77 | 78 | for url := range httpGetURLs() { 79 | n.Add(1) 80 | 81 | go func(url string) { 82 | defer n.Done() 83 | 84 | start := time.Now() 85 | value, err := m.Get(url) 86 | 87 | if err != nil { 88 | log.Print(err) 89 | return 90 | } 91 | 92 | fmt.Printf("%s, %s, %d bytes\n", 93 | url, time.Since(start), len(value.([]byte))) 94 | }(url) 95 | } 96 | 97 | n.Wait() 98 | } 99 | -------------------------------------------------------------------------------- /modules/mndlbrot/main.go: -------------------------------------------------------------------------------- 1 | // Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan. 2 | // License: https://creativecommons.org/licenses/by-nc-sa/4.0/ 3 | 4 | // See page 61. 5 | //!+ 6 | 7 | // Mandelbrot emits a PNG image of the Mandelbrot fractal. 8 | package main 9 | 10 | import ( 11 | "image" 12 | "image/color" 13 | "image/png" 14 | "math/cmplx" 15 | "os" 16 | ) 17 | 18 | func main() { 19 | const ( 20 | xmin, ymin, xmax, ymax = -2, -2, +2, +2 21 | width, height = 1024, 1024 22 | ) 23 | 24 | img := image.NewRGBA(image.Rect(0, 0, width, height)) 25 | for py := 0; py < height; py++ { 26 | y := float64(py)/height*(ymax-ymin) + ymin 27 | for px := 0; px < width; px++ { 28 | x := float64(px)/width*(xmax-xmin) + xmin 29 | z := complex(x, y) 30 | // Image point (px, py) represents complex value z. 31 | img.Set(px, py, mandelbrot(z)) 32 | } 33 | } 34 | png.Encode(os.Stdout, img) // NOTE: ignoring errors 35 | } 36 | 37 | func mandelbrot(z complex128) color.Color { 38 | const iterations = 200 39 | const contrast = 15 40 | 41 | var v complex128 42 | for n := uint8(0); n < iterations; n++ { 43 | v = v*v + z 44 | if cmplx.Abs(v) > 2 { 45 | return color.Gray{255 - contrast*n} 46 | } 47 | } 48 | return color.Black 49 | } 50 | 51 | //!- 52 | 53 | // Some other interesting functions: 54 | 55 | func acos(z complex128) color.Color { 56 | v := cmplx.Acos(z) 57 | blue := uint8(real(v)*128) + 127 58 | red := uint8(imag(v)*128) + 127 59 | return color.YCbCr{192, blue, red} 60 | } 61 | 62 | func sqrt(z complex128) color.Color { 63 | v := cmplx.Sqrt(z) 64 | blue := uint8(real(v)*128) + 127 65 | red := uint8(imag(v)*128) + 127 66 | return color.YCbCr{128, blue, red} 67 | } 68 | 69 | // f(x) = x^4 - 1 70 | // 71 | // z' = z - f(z)/f'(z) 72 | // = z - (z^4 - 1) / (4 * z^3) 73 | // = z - (z - 1/z^3) / 4 74 | func newton(z complex128) color.Color { 75 | const iterations = 37 76 | const contrast = 7 77 | for i := uint8(0); i < iterations; i++ { 78 | z -= (z - 1/(z*z*z)) / 4 79 | if cmplx.Abs(z*z*z*z-1) < 1e-6 { 80 | return color.Gray{255 - contrast*i} 81 | } 82 | } 83 | return color.Black 84 | } 85 | -------------------------------------------------------------------------------- /modules/lrucache/README.md: -------------------------------------------------------------------------------- 1 | # Least Recently Used (LRU) Cache 2 | 3 | 4 | ### Problem Description 5 | 6 | In computing, a *cache* is a component that stores data so that future requests for that data can be served faster. 7 | 8 | A *cache hit* occurs when the requested data can be found in a cache, while a *cache miss* occurs when it cannot. The *hit ratio* of a cache describes how often a searched-for item is actually available. 9 | 10 | *Cache replacement algorithms* are optimizing instructions that a program can utilize in order to manage a cache. When a cache is full, the algorithm must choose which items to discard to make room for the new ones. 11 | 12 | An *LRU cache* is a cache that uses one of the "Least Recently Used" family of cache replacement algorithms. LRU algorithms keep track of what was used when, and discard least recently used items first, using historical metadata in an attempt to maximize the hit ratio for better performance. 13 | 14 | Design a data structure that follows the constraints of a Least Recently Used (LRU) cache. 15 | 16 | Implement the `LRUCache` class: 17 | 18 | - `LRUCache(int capacity)` Initialize the LRU cache with size `capacity`. 19 | - `int get(int key)` Return the value of the `key` if the key exists, otherwise return `-1`. 20 | - `void put(int key, int value)` Update the value of the key if the key exists. Otherwise, add the key-value pair to the cache. If the number of keys exceeds the capacity, evict the least recently used key. 21 | - The functions get and put must each run in `O(1)` average time complexity. 22 | 23 | Example 1: 24 | ``` 25 | Input 26 | ["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"] 27 | [[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]] 28 | Output 29 | [null, null, null, 1, null, -1, null, -1, 3, 4] 30 | 31 | Explanation 32 | LRUCache lRUCache = new LRUCache(2); 33 | lRUCache.put(1, 1); // cache is {1=1} 34 | lRUCache.put(2, 2); // cache is {1=1, 2=2} 35 | lRUCache.get(1); // return 1 36 | lRUCache.put(3, 3); // LRU key was 2, evicts key 2, cache is {1=1, 3=3} 37 | lRUCache.get(2); // returns -1 (not found) 38 | lRUCache.put(4, 4); // LRU key was 1, evicts key 1, cache is {4=4, 3=3} 39 | lRUCache.get(1); // return -1 (not found) 40 | lRUCache.get(3); // return 3 41 | lRUCache.get(4); // return 4 42 | ``` 43 | Constraints: 44 | - `1 <= capacity <= 3000` 45 | - `0 <= key <= 104` 46 | - `0 <= value <= 105` 47 | - `At most 2 * 105 calls will be made to get and put.` 48 | -------------------------------------------------------------------------------- /modules/binsrch/go/binsrch/binsrch_test.go: -------------------------------------------------------------------------------- 1 | package binsrch 2 | 3 | import ( 4 | "testing" 5 | "math/rand" 6 | "time" 7 | ) 8 | 9 | func TestSearch(t *testing.T) { 10 | tests := []struct { 11 | points Points[int] 12 | abscissa int 13 | lower int 14 | upper int 15 | ok bool 16 | }{ 17 | // Basic case: abscissa present in the middle 18 | { 19 | points: Points[int]{{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}}, 20 | abscissa: 5, 21 | lower: 2, 22 | upper: 2, 23 | ok: true, 24 | }, 25 | // Case: abscissa present at the start 26 | { 27 | points: Points[int]{{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}}, 28 | abscissa: 1, 29 | lower: 0, 30 | upper: 0, 31 | ok: true, 32 | }, 33 | // Case: abscissa present at the end 34 | { 35 | points: Points[int]{{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}}, 36 | abscissa: 9, 37 | lower: 4, 38 | upper: 4, 39 | ok: true, 40 | }, 41 | // Case: abscissa not present 42 | { 43 | points: Points[int]{{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}}, 44 | abscissa: 0, 45 | ok: false, 46 | }, 47 | // Case: abscissa present multiple times 48 | { 49 | points: Points[int]{{1, 2}, {3, 4}, {5, 6}, {5, 7}, {9, 10}}, 50 | abscissa: 5, 51 | lower: 2, 52 | upper: 3, 53 | ok: true, 54 | }, 55 | // Case: empty points list 56 | { 57 | points: Points[int]{}, 58 | abscissa: 1, 59 | ok: false, 60 | }, 61 | } 62 | 63 | for _, test := range tests { 64 | actual, ok := test.points.WhereX(test.abscissa) 65 | 66 | if ok != test.ok { 67 | t.Errorf("got %v, %v -> %v, want %v", test.points, test.abscissa, ok, test.ok) 68 | } 69 | 70 | if ok == false { 71 | if actual != nil { 72 | t.Errorf("got %v, want nil", actual) 73 | } 74 | continue 75 | } 76 | 77 | expected := test.points[test.lower : test.upper+1] 78 | 79 | for index, expectedPoint := range expected { 80 | if len(actual) <= index { 81 | t.Errorf("got %v -> %v, want %v -> %v", test.points, actual, test.points, expected) 82 | break 83 | } 84 | actualPoint := actual[index] 85 | if expectedPoint != actualPoint { 86 | t.Errorf("got %v -> %v, want %v -> %v", test.points, actual, test.points, expected) 87 | break 88 | } 89 | } 90 | } 91 | } 92 | 93 | func BenchmarkSearch(b *testing.B) { 94 | rand.Seed(time.Now().UnixNano()) 95 | 96 | testCases := []struct { 97 | name string 98 | points Points[int] 99 | abscissa int 100 | }{ 101 | { 102 | name: "Small", 103 | points: GeneratePoints[int](1e6), 104 | abscissa: rand.Intn(1e6), 105 | }, 106 | { 107 | name: "Large", 108 | points: GeneratePoints[int](1e9), 109 | abscissa: rand.Intn(1e9), 110 | }, 111 | } 112 | 113 | for _, tc := range testCases { 114 | b.Run(tc.name, func(b *testing.B) { 115 | for i := 0; i < b.N; i++ { 116 | tc.points.WhereX(tc.abscissa) 117 | } 118 | }) 119 | } 120 | } 121 | 122 | func GeneratePoints[C Coordinate](n int) Points[C] { 123 | points := make(Points[C], n) 124 | for i := 0; i < n; i++ { 125 | points[i] = Point[C]{C(i), C(i)} 126 | } 127 | return points 128 | } 129 | -------------------------------------------------------------------------------- /modules/binsrch/go/binsrch_test.go: -------------------------------------------------------------------------------- 1 | package binsrch 2 | 3 | import ( 4 | "math/rand" 5 | "testing" 6 | "time" 7 | ) 8 | 9 | func TestSearch(t *testing.T) { 10 | tests := []struct { 11 | points Points[int] 12 | abscissa int 13 | lower int 14 | upper int 15 | ok bool 16 | }{ 17 | // Basic case: abscissa present in the middle 18 | { 19 | points: Points[int]{{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}}, 20 | abscissa: 5, 21 | lower: 2, 22 | upper: 2, 23 | ok: true, 24 | }, 25 | // Case: abscissa present at the start 26 | { 27 | points: Points[int]{{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}}, 28 | abscissa: 1, 29 | lower: 0, 30 | upper: 0, 31 | ok: true, 32 | }, 33 | // Case: abscissa present at the end 34 | { 35 | points: Points[int]{{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}}, 36 | abscissa: 9, 37 | lower: 4, 38 | upper: 4, 39 | ok: true, 40 | }, 41 | // Case: abscissa not present 42 | { 43 | points: Points[int]{{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}}, 44 | abscissa: 0, 45 | ok: false, 46 | }, 47 | // Case: abscissa present multiple times 48 | { 49 | points: Points[int]{{1, 2}, {3, 4}, {5, 6}, {5, 7}, {9, 10}}, 50 | abscissa: 5, 51 | lower: 2, 52 | upper: 3, 53 | ok: true, 54 | }, 55 | // Case: empty points list 56 | { 57 | points: Points[int]{}, 58 | abscissa: 1, 59 | ok: false, 60 | }, 61 | } 62 | 63 | for _, test := range tests { 64 | actual, ok := test.points.WhereX(test.abscissa) 65 | 66 | if ok != test.ok { 67 | t.Errorf("got %v, %v -> %v, want %v", test.points, test.abscissa, ok, test.ok) 68 | } 69 | 70 | if ok == false { 71 | if actual != nil { 72 | t.Errorf("got %v, want nil", actual) 73 | } 74 | continue 75 | } 76 | 77 | expected := test.points[test.lower : test.upper+1] 78 | 79 | for index, expectedPoint := range expected { 80 | if len(actual) <= index { 81 | t.Errorf("got %v -> %v, want %v -> %v", test.points, actual, test.points, expected) 82 | break 83 | } 84 | actualPoint := actual[index] 85 | if expectedPoint != actualPoint { 86 | t.Errorf("got %v -> %v, want %v -> %v", test.points, actual, test.points, expected) 87 | break 88 | } 89 | } 90 | } 91 | } 92 | 93 | type Case struct { 94 | name string 95 | m int 96 | n int 97 | points Points[int] 98 | abscissa int 99 | targets []int 100 | } 101 | 102 | func BenchmarkSearch(b *testing.B) { 103 | rand.Seed(time.Now().UnixNano()) 104 | 105 | cases := [2]Case{ 106 | { 107 | name: "Small [1k]", 108 | m: 1e3, 109 | n: 1e2, 110 | points: GeneratePoints[int](1e3), 111 | targets: GenerateTargets[int](1e3, 1e2), 112 | }, 113 | { 114 | name: "Large [100k]", 115 | m: 1e5, 116 | n: 1e2, 117 | points: GeneratePoints[int](1e5), 118 | targets: GenerateTargets[int](1e5, 1e2), 119 | }, 120 | } 121 | 122 | for i, c := range cases { 123 | b.Run(c.name, func(b *testing.B) { 124 | for b.Loop() { 125 | b.StopTimer() 126 | t := c.targets[i%c.n] 127 | b.StartTimer() 128 | c.points.WhereX(t) 129 | } 130 | }) 131 | } 132 | } 133 | 134 | func GeneratePoints[C Coordinate](n int) Points[C] { 135 | points := make(Points[C], n) 136 | for i := 0; i < n; i++ { 137 | points[i] = Point[C]{C(i), C(i)} 138 | } 139 | return points 140 | } 141 | 142 | func GenerateTargets[C Coordinate](m, n int) []C { 143 | targets := make([]C, n) 144 | 145 | for i := 0; i < n; i++ { 146 | targets[i] = C(rand.Intn(m)) 147 | } 148 | 149 | return targets 150 | } 151 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Mozilla Public License Version 2.0 2 | ================================== 3 | 4 | 1. Definitions 5 | -------------- 6 | 7 | 1.1. "Contributor" 8 | means each individual or legal entity that creates, contributes to 9 | the creation of, or owns Covered Software. 10 | 11 | 1.2. "Contributor Version" 12 | means the combination of the Contributions of others (if any) used 13 | by a Contributor and that particular Contributor's Contribution. 14 | 15 | 1.3. "Contribution" 16 | means Covered Software of a particular Contributor. 17 | 18 | 1.4. "Covered Software" 19 | means Source Code Form to which the initial Contributor has attached 20 | the notice in Exhibit A, the Executable Form of such Source Code 21 | Form, and Modifications of such Source Code Form, in each case 22 | including portions thereof. 23 | 24 | 1.5. "Incompatible With Secondary Licenses" 25 | means 26 | 27 | (a) that the initial Contributor has attached the notice described 28 | in Exhibit B to the Covered Software; or 29 | 30 | (b) that the Covered Software was made available under the terms of 31 | version 1.1 or earlier of the License, but not also under the 32 | terms of a Secondary License. 33 | 34 | 1.6. "Executable Form" 35 | means any form of the work other than Source Code Form. 36 | 37 | 1.7. "Larger Work" 38 | means a work that combines Covered Software with other material, in 39 | a separate file or files, that is not Covered Software. 40 | 41 | 1.8. "License" 42 | means this document. 43 | 44 | 1.9. "Licensable" 45 | means having the right to grant, to the maximum extent possible, 46 | whether at the time of the initial grant or subsequently, any and 47 | all of the rights conveyed by this License. 48 | 49 | 1.10. "Modifications" 50 | means any of the following: 51 | 52 | (a) any file in Source Code Form that results from an addition to, 53 | deletion from, or modification of the contents of Covered 54 | Software; or 55 | 56 | (b) any new file in Source Code Form that contains any Covered 57 | Software. 58 | 59 | 1.11. "Patent Claims" of a Contributor 60 | means any patent claim(s), including without limitation, method, 61 | process, and apparatus claims, in any patent Licensable by such 62 | Contributor that would be infringed, but for the grant of the 63 | License, by the making, using, selling, offering for sale, having 64 | made, import, or transfer of either its Contributions or its 65 | Contributor Version. 66 | 67 | 1.12. "Secondary License" 68 | means either the GNU General Public License, Version 2.0, the GNU 69 | Lesser General Public License, Version 2.1, the GNU Affero General 70 | Public License, Version 3.0, or any later versions of those 71 | licenses. 72 | 73 | 1.13. "Source Code Form" 74 | means the form of the work preferred for making modifications. 75 | 76 | 1.14. "You" (or "Your") 77 | means an individual or a legal entity exercising rights under this 78 | License. For legal entities, "You" includes any entity that 79 | controls, is controlled by, or is under common control with You. For 80 | purposes of this definition, "control" means (a) the power, direct 81 | or indirect, to cause the direction or management of such entity, 82 | whether by contract or otherwise, or (b) ownership of more than 83 | fifty percent (50%) of the outstanding shares or beneficial 84 | ownership of such entity. 85 | 86 | 2. License Grants and Conditions 87 | -------------------------------- 88 | 89 | 2.1. Grants 90 | 91 | Each Contributor hereby grants You a world-wide, royalty-free, 92 | non-exclusive license: 93 | 94 | (a) under intellectual property rights (other than patent or trademark) 95 | Licensable by such Contributor to use, reproduce, make available, 96 | modify, display, perform, distribute, and otherwise exploit its 97 | Contributions, either on an unmodified basis, with Modifications, or 98 | as part of a Larger Work; and 99 | 100 | (b) under Patent Claims of such Contributor to make, use, sell, offer 101 | for sale, have made, import, and otherwise transfer either its 102 | Contributions or its Contributor Version. 103 | 104 | 2.2. Effective Date 105 | 106 | The licenses granted in Section 2.1 with respect to any Contribution 107 | become effective for each Contribution on the date the Contributor first 108 | distributes such Contribution. 109 | 110 | 2.3. Limitations on Grant Scope 111 | 112 | The licenses granted in this Section 2 are the only rights granted under 113 | this License. No additional rights or licenses will be implied from the 114 | distribution or licensing of Covered Software under this License. 115 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 116 | Contributor: 117 | 118 | (a) for any code that a Contributor has removed from Covered Software; 119 | or 120 | 121 | (b) for infringements caused by: (i) Your and any other third party's 122 | modifications of Covered Software, or (ii) the combination of its 123 | Contributions with other software (except as part of its Contributor 124 | Version); or 125 | 126 | (c) under Patent Claims infringed by Covered Software in the absence of 127 | its Contributions. 128 | 129 | This License does not grant any rights in the trademarks, service marks, 130 | or logos of any Contributor (except as may be necessary to comply with 131 | the notice requirements in Section 3.4). 132 | 133 | 2.4. Subsequent Licenses 134 | 135 | No Contributor makes additional grants as a result of Your choice to 136 | distribute the Covered Software under a subsequent version of this 137 | License (see Section 10.2) or under the terms of a Secondary License (if 138 | permitted under the terms of Section 3.3). 139 | 140 | 2.5. Representation 141 | 142 | Each Contributor represents that the Contributor believes its 143 | Contributions are its original creation(s) or it has sufficient rights 144 | to grant the rights to its Contributions conveyed by this License. 145 | 146 | 2.6. Fair Use 147 | 148 | This License is not intended to limit any rights You have under 149 | applicable copyright doctrines of fair use, fair dealing, or other 150 | equivalents. 151 | 152 | 2.7. Conditions 153 | 154 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 155 | in Section 2.1. 156 | 157 | 3. Responsibilities 158 | ------------------- 159 | 160 | 3.1. Distribution of Source Form 161 | 162 | All distribution of Covered Software in Source Code Form, including any 163 | Modifications that You create or to which You contribute, must be under 164 | the terms of this License. You must inform recipients that the Source 165 | Code Form of the Covered Software is governed by the terms of this 166 | License, and how they can obtain a copy of this License. You may not 167 | attempt to alter or restrict the recipients' rights in the Source Code 168 | Form. 169 | 170 | 3.2. Distribution of Executable Form 171 | 172 | If You distribute Covered Software in Executable Form then: 173 | 174 | (a) such Covered Software must also be made available in Source Code 175 | Form, as described in Section 3.1, and You must inform recipients of 176 | the Executable Form how they can obtain a copy of such Source Code 177 | Form by reasonable means in a timely manner, at a charge no more 178 | than the cost of distribution to the recipient; and 179 | 180 | (b) You may distribute such Executable Form under the terms of this 181 | License, or sublicense it under different terms, provided that the 182 | license for the Executable Form does not attempt to limit or alter 183 | the recipients' rights in the Source Code Form under this License. 184 | 185 | 3.3. Distribution of a Larger Work 186 | 187 | You may create and distribute a Larger Work under terms of Your choice, 188 | provided that You also comply with the requirements of this License for 189 | the Covered Software. If the Larger Work is a combination of Covered 190 | Software with a work governed by one or more Secondary Licenses, and the 191 | Covered Software is not Incompatible With Secondary Licenses, this 192 | License permits You to additionally distribute such Covered Software 193 | under the terms of such Secondary License(s), so that the recipient of 194 | the Larger Work may, at their option, further distribute the Covered 195 | Software under the terms of either this License or such Secondary 196 | License(s). 197 | 198 | 3.4. Notices 199 | 200 | You may not remove or alter the substance of any license notices 201 | (including copyright notices, patent notices, disclaimers of warranty, 202 | or limitations of liability) contained within the Source Code Form of 203 | the Covered Software, except that You may alter any license notices to 204 | the extent required to remedy known factual inaccuracies. 205 | 206 | 3.5. Application of Additional Terms 207 | 208 | You may choose to offer, and to charge a fee for, warranty, support, 209 | indemnity or liability obligations to one or more recipients of Covered 210 | Software. However, You may do so only on Your own behalf, and not on 211 | behalf of any Contributor. You must make it absolutely clear that any 212 | such warranty, support, indemnity, or liability obligation is offered by 213 | You alone, and You hereby agree to indemnify every Contributor for any 214 | liability incurred by such Contributor as a result of warranty, support, 215 | indemnity or liability terms You offer. You may include additional 216 | disclaimers of warranty and limitations of liability specific to any 217 | jurisdiction. 218 | 219 | 4. Inability to Comply Due to Statute or Regulation 220 | --------------------------------------------------- 221 | 222 | If it is impossible for You to comply with any of the terms of this 223 | License with respect to some or all of the Covered Software due to 224 | statute, judicial order, or regulation then You must: (a) comply with 225 | the terms of this License to the maximum extent possible; and (b) 226 | describe the limitations and the code they affect. Such description must 227 | be placed in a text file included with all distributions of the Covered 228 | Software under this License. Except to the extent prohibited by statute 229 | or regulation, such description must be sufficiently detailed for a 230 | recipient of ordinary skill to be able to understand it. 231 | 232 | 5. Termination 233 | -------------- 234 | 235 | 5.1. The rights granted under this License will terminate automatically 236 | if You fail to comply with any of its terms. However, if You become 237 | compliant, then the rights granted under this License from a particular 238 | Contributor are reinstated (a) provisionally, unless and until such 239 | Contributor explicitly and finally terminates Your grants, and (b) on an 240 | ongoing basis, if such Contributor fails to notify You of the 241 | non-compliance by some reasonable means prior to 60 days after You have 242 | come back into compliance. Moreover, Your grants from a particular 243 | Contributor are reinstated on an ongoing basis if such Contributor 244 | notifies You of the non-compliance by some reasonable means, this is the 245 | first time You have received notice of non-compliance with this License 246 | from such Contributor, and You become compliant prior to 30 days after 247 | Your receipt of the notice. 248 | 249 | 5.2. If You initiate litigation against any entity by asserting a patent 250 | infringement claim (excluding declaratory judgment actions, 251 | counter-claims, and cross-claims) alleging that a Contributor Version 252 | directly or indirectly infringes any patent, then the rights granted to 253 | You by any and all Contributors for the Covered Software under Section 254 | 2.1 of this License shall terminate. 255 | 256 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 257 | end user license agreements (excluding distributors and resellers) which 258 | have been validly granted by You or Your distributors under this License 259 | prior to termination shall survive termination. 260 | 261 | ************************************************************************ 262 | * * 263 | * 6. Disclaimer of Warranty * 264 | * ------------------------- * 265 | * * 266 | * Covered Software is provided under this License on an "as is" * 267 | * basis, without warranty of any kind, either expressed, implied, or * 268 | * statutory, including, without limitation, warranties that the * 269 | * Covered Software is free of defects, merchantable, fit for a * 270 | * particular purpose or non-infringing. The entire risk as to the * 271 | * quality and performance of the Covered Software is with You. * 272 | * Should any Covered Software prove defective in any respect, You * 273 | * (not any Contributor) assume the cost of any necessary servicing, * 274 | * repair, or correction. This disclaimer of warranty constitutes an * 275 | * essential part of this License. No use of any Covered Software is * 276 | * authorized under this License except under this disclaimer. * 277 | * * 278 | ************************************************************************ 279 | 280 | ************************************************************************ 281 | * * 282 | * 7. Limitation of Liability * 283 | * -------------------------- * 284 | * * 285 | * Under no circumstances and under no legal theory, whether tort * 286 | * (including negligence), contract, or otherwise, shall any * 287 | * Contributor, or anyone who distributes Covered Software as * 288 | * permitted above, be liable to You for any direct, indirect, * 289 | * special, incidental, or consequential damages of any character * 290 | * including, without limitation, damages for lost profits, loss of * 291 | * goodwill, work stoppage, computer failure or malfunction, or any * 292 | * and all other commercial damages or losses, even if such party * 293 | * shall have been informed of the possibility of such damages. This * 294 | * limitation of liability shall not apply to liability for death or * 295 | * personal injury resulting from such party's negligence to the * 296 | * extent applicable law prohibits such limitation. Some * 297 | * jurisdictions do not allow the exclusion or limitation of * 298 | * incidental or consequential damages, so this exclusion and * 299 | * limitation may not apply to You. * 300 | * * 301 | ************************************************************************ 302 | 303 | 8. Litigation 304 | ------------- 305 | 306 | Any litigation relating to this License may be brought only in the 307 | courts of a jurisdiction where the defendant maintains its principal 308 | place of business and such litigation shall be governed by laws of that 309 | jurisdiction, without reference to its conflict-of-law provisions. 310 | Nothing in this Section shall prevent a party's ability to bring 311 | cross-claims or counter-claims. 312 | 313 | 9. Miscellaneous 314 | ---------------- 315 | 316 | This License represents the complete agreement concerning the subject 317 | matter hereof. If any provision of this License is held to be 318 | unenforceable, such provision shall be reformed only to the extent 319 | necessary to make it enforceable. Any law or regulation which provides 320 | that the language of a contract shall be construed against the drafter 321 | shall not be used to construe this License against a Contributor. 322 | 323 | 10. Versions of the License 324 | --------------------------- 325 | 326 | 10.1. New Versions 327 | 328 | Mozilla Foundation is the license steward. Except as provided in Section 329 | 10.3, no one other than the license steward has the right to modify or 330 | publish new versions of this License. Each version will be given a 331 | distinguishing version number. 332 | 333 | 10.2. Effect of New Versions 334 | 335 | You may distribute the Covered Software under the terms of the version 336 | of the License under which You originally received the Covered Software, 337 | or under the terms of any subsequent version published by the license 338 | steward. 339 | 340 | 10.3. Modified Versions 341 | 342 | If you create software not governed by this License, and you want to 343 | create a new license for such software, you may create and use a 344 | modified version of this License if you rename the license and remove 345 | any references to the name of the license steward (except to note that 346 | such modified license differs from this License). 347 | 348 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 349 | Licenses 350 | 351 | If You choose to distribute Source Code Form that is Incompatible With 352 | Secondary Licenses under the terms of this version of the License, the 353 | notice described in Exhibit B of this License must be attached. 354 | 355 | Exhibit A - Source Code Form License Notice 356 | ------------------------------------------- 357 | 358 | This Source Code Form is subject to the terms of the Mozilla Public 359 | License, v. 2.0. If a copy of the MPL was not distributed with this 360 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 361 | 362 | If it is not possible or desirable to put the notice in a particular 363 | file, then You may include the notice in a location (such as a LICENSE 364 | file in a relevant directory) where a recipient would be likely to look 365 | for such a notice. 366 | 367 | You may add additional accurate notices of copyright ownership. 368 | 369 | Exhibit B - "Incompatible With Secondary Licenses" Notice 370 | --------------------------------------------------------- 371 | 372 | This Source Code Form is "Incompatible With Secondary Licenses", as 373 | defined by the Mozilla Public License, v. 2.0. 374 | --------------------------------------------------------------------------------