Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/kdungs/cpp-prelude

Implement functionality from Haskell's Prelude in C++11. Just for fun. Errors guaranteed.
https://github.com/kdungs/cpp-prelude

exercise

Last synced: 8 days ago
JSON representation

Implement functionality from Haskell's Prelude in C++11. Just for fun. Errors guaranteed.

Awesome Lists containing this project

README

        

# cpp-prelude
[![Build Status](https://travis-ci.org/kdungs/cpp-prelude.svg?branch=master)](https://travis-ci.org/kdungs/cpp-prelude) ← _This doesn't mean that everything is all right!_

Implement functionality from Haskell's Prelude in C++11. Just for fun. Errors guaranteed.

After playing around with [functional immutable lists in C++11](https://github.com/kdungs/cpp-list), I though it would be nice to have the functionality from Haskell's Prelude (or at least the interesting functions) in C++. This is an attempt to implement some of the functionality on STL containers. Whenever possible, functionality from the STL is used.

## Functionality

### List operations

* [x] `map :: (a -> b) -> [a] -> [b]`
* [x] `(++) :: [a] -> [a] -> [a]` as `join`
* [x] `filter :: (a -> Bool) -> [a] -> [a]`
* [x] `head :: [a] -> a`
* [x] `last :: [a] -> a`
* [x] `tail :: [a] -> [a]`
* [x] `init :: [a] -> [a]`
* [x] `null :: [a] -> Bool`
* [x] `length :: [a] -> Int`
* [x] `(!!) :: [a] -> Int -> a` as `at`
* [x] `reverse :: [a] -> [a]`

### Reducing lists (folds)

* [x] `foldl :: (b -> a -> b) -> b -> [a] -> b`
* [x] `foldl1 :: (a -> a -> a) -> [a] -> a`
* [x] `foldr :: (a -> b -> b) -> b -> [a] -> b`
* [x] `foldr1 :: (a -> a -> a) -> [a] -> a`

### Special folds

* [x] `and :: [Bool] -> Bool`
* [x] `or :: [Bool] -> Bool`
* [x] `any :: (a -> Bool) -> [a] -> Bool`
* [x] `all :: (a -> Bool) -> [a] -> Bool`
* [x] `sum :: Num a => [a] -> a`
* [x] `product :: Num a => [a] -> a`
* [x] `concat :: [[a]] -> [a]`
* [x] `concatMap :: (a -> [b]) -> [a] -> [b]`
* [x] `maximum :: Ord a => [a] -> a`
* [x] `minimum :: Ord a => [a] -> a`

### Building lists

* [ ] `scanl :: (b -> a -> b) -> b -> [a] -> [b]`
* [ ] `scanl1 :: (a -> a -> a) -> [a] -> [a]`
* [ ] `scanr :: (a -> b -> b) -> b -> [a] -> [b]`
* [ ] `scanr1 :: (a -> a -> a) -> [a] -> [a]`

### Infinite lists

Without lazy evaluation, there is not really any way to produce infinite
lists. Maybe I'll implement these functions with an additional size
parameter.

### Sublists

* [x] `take :: Int -> [a] -> [a]`
* [x] `drop :: Int -> [a] -> [a]`
* [x] `splitAt :: Int -> [a] -> ([a], [a])`
* [x] `takeWhile :: (a -> Bool) -> [a] -> [a]`
* [x] `dropWhile :: (a -> Bool) -> [a] -> [a]`
* [x] `span :: (a -> Bool) -> [a] -> ([a], [a])`
* [x] `break :: (a -> Bool) -> [a] -> ([a], [a])`

### Searching lists

* [ ] `elem :: Eq a => a -> [a] -> Bool`
* [ ] `notElem :: Eq a => a -> [a] -> Bool`
* [ ] `lookup :: Eq a => a -> [(a, b)] -> Maybe b`

### Zipping and unzipping lists

* [x] `zip :: [a] -> [b] -> [(a, b)]`
* [x] `zip3 :: [a] -> [b] -> [c] -> [(a, b, c)]`
* [x] `zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]`
* [x] `zipWith3 :: (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d]`
* [x] `unzip :: [(a, b)] -> ([a], [b])`
* [x] `unzip3 :: [(a, b, c)] -> ([a], [b], [c])`