Example #1
0
func (b *Block) GenerateMerkelRoot() []byte {

	var merkell func(hashes [][]byte) []byte
	merkell = func(hashes [][]byte) []byte {

		l := len(hashes)
		if l == 0 {
			return nil
		}
		if l == 1 {
			return hashes[0]
		} else {

			if l%2 == 1 {
				return merkell([][]byte{merkell(hashes[:l-1]), hashes[l-1]})
			}

			bs := make([][]byte, l/2)
			for i, _ := range bs {
				j, k := i*2, (i*2)+1
				bs[i] = helpers.SHA256(append(hashes[j], hashes[k]...))
			}
			return merkell(bs)
		}
	}

	ts := functional.Map(func(t Transaction) []byte { return t.Hash() }, []Transaction(*b.TransactionSlice)).([][]byte)
	return merkell(ts)

}
Example #2
0
func draw(r Race) {

	drawing := f.Map(func(car Car) string {

		return f.Reduce(func(a string, i int) string {

			return a + "-"

		}, make([]int, car.Position), "").(string)

	}, r.Cars).([]string)

	fmt.Println("............................")
	f.Map(func(a string) interface{} {

		fmt.Println(a)
		return a
	}, drawing)
}
Example #3
0
func main() {

	words := []string{"doing", "go", "functional", "programming"}
	numbers := []int{1, 2, 3}

	fmt.Println("MAP")

	fmt.Println("Square", numbers, f.Map(func(x int) int { return x * x }, numbers))
	fmt.Println("Length", words, f.Map(func(x string) int { return len(x) }, words))
	fmt.Println("Hash", words, f.Map(hash, words))

	fmt.Println("REDUCE")

	fmt.Println("Sum", numbers, f.Reduce(func(a int, x int) int { return a + x }, numbers, 0))
	fmt.Println("Total length", words, f.Reduce(func(a int, x string) int { return a + len(x) }, words, 0))

	fmt.Println("FILTER")
	fmt.Println("Long words (>5)", words, f.Filter(func(x string) bool { return (len(x) > 5) }, words).([]string))

}
Example #4
0
func carState(cars []Car) []Car {

	return f.Map(func(c Car) Car {

		if helpers.RandomInt(0, 3) == 0 {

			return Car{Position: c.Position + 1}
		}

		return Car{Position: c.Position}

	}, cars).([]Car)
}
Example #5
0
func main() {

	root, err := GitDirectoryRoot()

	if err != nil {

		fmt.Println("You must use todos inside a git repository")

	} else {

		if len(flag.Args()) < 1 {
			showHelp()
		} else {

			mode := flag.Args()[0]
			switch mode {
			case "setup":
				setup(root)

			case "work":

				// Try to read lines from Stdin, if not talk to git
				diff, err := ReadStdin()
				if len(diff) == 0 {

					diff, err = GitDiffFiles()
					logOnError(err)
				}

				diff = functional.Map(func(s string) string { return path.Join(root, s) }, diff).([]string)

				work(root, diff)

			default:
				showHelp()
			}
		}
	}
}