• Stars
    star
    139
  • Rank 262,954 (Top 6 %)
  • Language
    Go
  • License
    MIT License
  • Created over 5 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Haskell-flavoured functions for Go πŸ˜ƒ

Hasgo Mentioned in Awesome Go Build Status

Hasgo is a code generator with functions influenced by Haskell. It comes with some types out-of-the-box so you can start using it without running the generator. Specifically you can start using Hasgo's Strings and Ints types.

We want to focus on being:

  • Immutable
  • Strongly-Typed (no interface{})
  • Nil-safe

Pie

The inspiration for Hasgo, as well as some ideas around implementation come from the lovely Pie library, made by Elliot Chance. It's safe to say that Hasgo would not exist without Pie. However, the way Pie and Hasgo work is not the same and neither is the focus of the project. If you don't find a function in Hasgo, check out Pie! πŸ˜ƒ

Example

import . "github.com/DylanMeeus/hasgo/types"
func EpicFunction() {
	// create a range of -10 -> 10. Take the absolute values, keep only even numbers, and sum them.
	result := IntRange(-10,10).
		Abs().
		Filter(func(i int64) bool {
			return i % 2 == 0
		}).
		Sum()
	// result = 60 
}

You can find more examples here.

Installation

go get -u github.com/DylanMeeus/hasgo

Or add hasgo to your go.mod file.

require github.com/DylanMeeus/hasgo/v1.0.2

Types

  • Ints ([]int64)
  • Strings ([]string)

Functions

These are the function currently available with Hasgo. It shows you which type of data they operate on as well as the Haskell type definition. The first symbol of the signature is actually the method receiver in Go terms.

Alternatively, you can consult the godoc

Generic functions

These functions can be generated for every type.

Function Signature String Number Struct Description
Abs [a] -> [a] βœ“ Return a slice containing the absolute values
All [a] -> (a -> bool) -> bool βœ“ βœ“ βœ“ Returns true if the predicate applies to all elements in the slice
Any [a] -> (a -> bool) -> bool βœ“ βœ“ βœ“ Returns true if one or more elements satisfy the predicate
Average [a] -> a βœ“ Returns the average of all elements
Break (a -> bool) -> [a] -> ([a], [a]) βœ“ βœ“ βœ“ Returns a tuple of all elements until the first one that matches the predicate, followed by the remaining elements.
Delete [a] -> a -> [a] βœ“ βœ“ βœ“ Returns the slice with the first occurance of the element deleted.
Drop Int -> [a] -> [a] βœ“ βœ“ βœ“ Returns the suffix of xs after the first n elements.
DropWhile (a -> bool) -> [a] -> [a] βœ“ βœ“ βœ“ Returns the suffix of xs after the predicate's first failure.
Elem [a] -> a -> bool βœ“ βœ“ βœ“ Returns true if the slice contains the element.
Filter [a] -> (a -> bool) -> [a] βœ“ βœ“ βœ“ Filter the slice based on a predicate
Foldl [a] -> a -> (a -> a -> a) -> a βœ“ βœ“ βœ“ Left fold over the slice to reduce it to one element with starting value.
Foldl1 [a] -> (a -> a -> a) -> a βœ“ βœ“ βœ“ Left fold over the slice to reduce it to one element.
Foldr [a] -> b -> (a -> b -> b) -> b βœ“ βœ“ βœ“ Right fold over the slice to reduce it to one element with a starting value.
Foldr1 [a] -> (a -> a -> a) -> a βœ“ βœ“ βœ“ Right fold over the slice to reduce it to one element.
Group [a] -> [[a]] βœ“ βœ“ βœ“ Returns a list of lists where each list contains grouped values from the input list.
Head [a] -> a βœ“ βœ“ βœ“ Return the first element
Init [a] -> [a] βœ“ βœ“ βœ“ Returns all elements minus the last
Inits [a] -> [[a]] βœ“ βœ“ βœ“ Returns all initial segments of the slice, shortest first.
Intercalate [a] -> [[a]] -> [a] βœ“ βœ“ βœ“ Intersperses the slice in between the provided 2d-slice
Intersperse [a] -> a -> [a] βœ“ βœ“ βœ“ Intersperses the value in between all elements of the provided slice
IsPrefixOf [a] -> [a] -> bool βœ“ βœ“ βœ“ Returns true if the current slice is a prefix of the provided slice
Last [a] -> a βœ“ βœ“ βœ“ Returns the last element
Length [a] -> int βœ“ βœ“ βœ“ Returns the length of the slice
Map [a] -> (a -> a) -> [a] βœ“ βœ“ βœ“ Returns a slice with the function applied to each element of the input
Maximum [a] -> a βœ“ Returns the largest element
MaximumBy [a] -> (a -> a) -> a -> a βœ“ βœ“ βœ“ Returns the maximum element according to comparator
Minimum [a] -> a βœ“ Returns the lowest element
Modes [a] -> [a] βœ“ βœ“ βœ“ Returns the elements with the highest frequency
Nub [a] -> [a] βœ“ βœ“ βœ“ Returns a Slice containing one of each of the input elements
Null [a] -> bool βœ“ βœ“ βœ“ Returns true if the slice is empty, false otherwise
Product [a] -> a βœ“ Returns the product of all elements in the slice.
Reverse [a] -> [a] βœ“ βœ“ βœ“ Returns a slice with the elements reversed
Scanl [a] -> b -> (a -> b -> a) -> [b] βœ“ βœ“ βœ“ Left fold over the slice to reduce it to one element with a starting value and return every iteration in a slice.
Sort [a] -> [a] βœ“ βœ“ Returns a sorted slice (original remains unsorted)
Span (a -> bool) -> [a] -> ([a], [a]) βœ“ βœ“ βœ“ Returns a tuple of all elements until the first one that does not match the predicate, followed by the remaining elements.
SplitAt Int -> [a] -> ([a], [a]) βœ“ βœ“ βœ“ Returns a tuple with all elements up until the specified index, followed by the elements after the index.
Sum [a] -> a βœ“ βœ“ βœ“ The sum of elements in the slice
Tail [a] -> [a] βœ“ βœ“ βœ“ Returns all elements minus the first
Tails [a] -> [[a]] βœ“ βœ“ βœ“ Returns all final segments of the slice, longest first.
Take [a] -> uint64 -> [a] βœ“ βœ“ βœ“ Take N elements from the slice, or all if N exceeds the length.
TakeWhile [a] -> (a -> bool) -> [a] βœ“ βœ“ βœ“ Take all elements until the first one that does not match the predicate.
Uncons [a] -> (a, [a]) βœ“ βœ“ βœ“ Returns a tuple of the head and tail of the slice
Unlines [a] -> string βœ“ βœ“ βœ“ Returns a newline separated string of all elements in the slice
Unwords [a] -> string βœ“ βœ“ βœ“ Returns a space-separated string of all elements in the slice

Hardcoded functions

The built-in types (Strings, Ints, Bools) have some functions defined on them that are not generated. Mostly because we could not create them in a generic way.

Type Function Signature Description
Ints Equals *Ints -> Ints -> bool Returns true if both slices contain the same elements
Ints EqualsOrdered *Ints -> Ints -> bool Returns true if both slices contain the same elements, in the same position
Ints IntRange int64 -> int64 -> Ints Return an integer range from [start,stop]
Ints IntReplicate uint64 -> int64 -> Ints Return a slice with the input element repeated n times
Strings Equals *Strings -> Strings -> bool Returns true if both slices contain the same elements
Strings EqualsOrdered *Strings -> Strings -> bool Returns true if both slices contain the same elements, in the same position
Strings Lines string -> Strings Returns Strings separated by a newline.
Strings StringReplicate uint64 -> string -> Strings Return a slice with the input element repeated n times
Strings Words string -> Strings Returns Strings separated by a space.
Bools And Bools -> bool Returns true if all bools are true.
Bools Or Bools -> bool Returns true if any bool is true.

* (Functions prefixed by a star are functions added to the type itself, where first element in the signature is the method receiver. So for examples, the Equals method is Ints{1,2}.Equals(Ints{1}). But, the IntRange function looks like hasgo.IntRange(0,10).

Contributing

You can help out Hasgo in a variety of ways! Here are some ideas:

  • Use Hasgo! πŸ˜ƒ
  • Spread the word (Write a blog, tweet, talk about..)
  • Suggest features (Create an issue to make a suggestion)
  • Report bugs (Similarly, create an issue)
  • Contribute code. (Create a PR, we'll gladly take a look and help you get it merged!)

What's in a name?

The name Hasgo is a portmanteau of "Haskell" and "Go". I'm a big fan of both languages, though they are quite different. It's impossible to write real Haskell-like code in Go. There are some obvious differences between the languages in terms of syntax. I hope the functions in this library stay as close as possible to their Haskell implementations. There might be extra functions in here that are not in Haskell, and there will be functions in Haskell that you won't find here.

The inspiration mainly shows in the naming of functions. If the functions were named after Java lambdas, it'd be called "Jago". Sorry if you expected more Haskell goodness (I'm open to suggestions of how more haskell in Hasgo!)

Real Generics?

Currently I have an experimental implementation of hasgo here as hasgo2. It does require a development version of Go installed from source to function correctly at this stage.

More Repositories

1

GoAudio

Go tools for audio processing & creation 🎢
Go
277
star
2

MediumCode

Code for medium posts
Go
30
star
3

hasgo2

Hasgo, but with actual generics
Go
11
star
4

IntelliJ-Xkcd-Startup

Shows a random XKCD comic when an IntelliJ project is opened
Java
9
star
5

SwingChromecast

Swing client to interact with your chromecasts
Java
8
star
6

QuantumRandom

Generate quantum random data using qrng.anu.edu
Go
6
star
7

ImageF-ck

Brainf*ck variant using images as sourcecode
Python
6
star
8

AdventOfCode

Repository for advent of code
Go
4
star
9

ArduinoNano_KeyInput

Keyboard input via Arduino Nano
Python
3
star
10

Ubuntu16.04_devkitpro_gba

Setup to build gameboy advance files on Ubuntu 16.04 (LTS).
C++
3
star
11

GoPlay

Playground for GO
JavaScript
2
star
12

Spotify_Playlist_Analyser

Create some charts based on data from a users playlist.
Python
2
star
13

ghcloner

Clones all your github repositories, sorted by detected language πŸ‘
Python
2
star
14

IntelliJ-config

My IntelliJ config
1
star
15

ScrabbleWordFinder

Finds possible words given a sequence of letters. Written in Haskell
Haskell
1
star
16

CNOGame

A repo for my CodenameOne game
Java
1
star
17

go-server-browser

A Go implementation of the Valve Master Server Protocol
Go
1
star
18

HaskellTsvCsvComparison

Compare tsv and csv files
Haskell
1
star
19

10fastfingers-mod

Chrome plugin to change the https://10fastfingers.com/typing-test/ website.
HTML
1
star
20

lunopher

Go
1
star
21

InsaniText

Text editor written in Python with PyQt
Python
1
star
22

LispPlayground

Playing around with (common) lisp stuff :)
Common Lisp
1
star
23

PascalTrianglePyramid

Repo for a pascal triangle / pyramid
JavaScript
1
star
24

TypingWorld

TypingWorld backend and frontend
Java
1
star
25

DylanMeeus.github.io

github pages website
HTML
1
star
26

RustPlayground

Makefile
1
star
27

CompetitiveProgramming3

Solutions to excercises in the book "Competitive Programming 3"
Python
1
star