Skip to content

Commit

Permalink
Refs #13. Stewing on docstring format
Browse files Browse the repository at this point in the history
  • Loading branch information
elimirks committed Nov 14, 2020
1 parent cc3cdc9 commit bbf0213
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions mylib/postlude/maybe.my
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,46 @@ def listToMaybe(Nil):
def listToMaybe(head|_):
return Just(head)


#| Converts a maybe into a list
#|
#| @example
#| >>> maybeToList(Just(2))
#| [2]
#|
#| @example
#| >>> maybeToList(None)
#| []
#|
#| @param #1 Maybe(a)
#| @return [a]
def maybeToList(None):
return []

def maybeToList(Just(x)):
return [x]


#| Creates a new list containing only the Just values contained in the input list.
#|
#| @example
#| >>> catMaybes([Just(2), Nothing, Just(4)])
#| [2, 4]
#|
#| @param maybeList [Maybe(a)]
#| @return [a]
def catMaybes(maybeList):
return maybeList.bind(maybeToList)


#| Apply a function that returns maybes to each element of the list.
#| Then create a new list containing only the Just values from that list.
#|
#| @example
#| >>> [1, 2, 3, 4, 5].mapMaybe(
#| (x): if x < 4: Just(x) else: None
#| )
#| [1, 2, 3]
#|
#| @param inputList [a]
#| @param fun (a): Maybe(b)
#| @return [b]
def mapMaybe(inputList, fun):
return inputList.map(fun).catMaybes()

0 comments on commit bbf0213

Please sign in to comment.