- Basic Functions
- ++
- head
- last
- tail
- init
- uncons
- singleton
- null
- List transformers
- intersperse
- intercalate
- transepose
- subsequences
- permutations
- Building list
- scanl
- scanl1
- scanr
- scanr1
- Infinite list
- iterate
- repeat
- replicate
- cycle
- Sublists
- take
- drop
- splitAt
- takeWhile
- dropWhile
- span
- breakList
- stripPrefix
- inits
- tails
- Predicates
- isPrefixOf
- isSuffixOf
- isInfixOf
- Searching lists
- elem
- notElem
- lookup
- Searching with a predicate
- find
- partition
Procs
proc `!!`(x: string; i: int): char {....raises: [], tags: [].}
proc `!!`[T](x: seq[T]; i: int): T
proc `++`(x: string; y: string): string {....raises: [], tags: [].}
-
Example:
doAssert "abc" ++ "def" == "abcdef"
proc `++`[T](x: seq[T]; y: seq[T]): seq[T]
-
Append two lists
Example:
doAssert @[1,2,3] ++ @[4,5,6] == @[1,2,3,4,5,6] doAssert @["abc","def"] ++ @["ghi","jkl"] == @["abc","def","ghi","jkl"] doAssert @[1.1,2.2,3.3] ++ @[4.4,5.5,6.6] == @[1.1,2.2,3.3,4.4,5.5,6.6]
proc breakList[T](f: proc (a: T): bool; xs: seq[T]): (seq[T], seq[T])
-
Example:
import sugar doAssert breakList((x:int)->bool=>x<3 ,@[1,2,3,4,1,2,3,4]) == (@[], @[1, 2, 3, 4, 1, 2, 3, 4]) doAssert breakList((x:int)->bool=>x<0 ,@[1,2,3]) == (@[1, 2, 3], @[])
proc breakList[T](f: proc (a: T): bool; xs: string): (string, string)
-
Example:
import sugar doAssert breakList((x:char)->bool=>x>'d' ,"abcdefg") == ("abcd", "efg")
proc concatMap[T](xs: seq[seq[T]]; f: proc; x: T): seq[T]
-
Example:
func plus(a,b:int):int=return a+b func minus(a,b:int):int=return a-b func product(a,b:float):float=return a*b func divide(a,b:int):int = a div b func floatDiv(a,b:float):float = a/b doAssert @[@[1,2,3],@[4,5,6]].concatMap(plus,1) == @[2,3,4,5,6,7] doAssert @[@[1,2,3],@[4,5,6]].concatMap(minus,1) == @[0,1,2,3,4,5] doAssert @[@[1,2,3],@[4,5,6]].concatMap(divide,2) == @[0,1,1,2,2,3] doAssert @[@[1.1,2.2,3.3],@[4.4,5.5,10.0]].concatMap(product,2.0) == @[2.2,4.4,6.6,8.8,11.0,20.0] doAssert @[@[1.0,2.0,3.0],@[4.0,5.0,6.0]].concatMap(floatDiv,2) == @[0.5,1.0,1.5,2.0,2.5,3.0]
proc drop(n: int; xs: string): string {....raises: [], tags: [].}
-
Example:
doAssert drop(2,"123456") == "3456" doAssert drop(3,"123") == ""
proc drop[T](n: int; xs: seq[T]): seq[T]
-
Example:
doAssert drop(3,@[1,2,3,4,5]) == @[4,5] doAssert drop(3,@[1,2,3]) == @[]
proc dropWhile[T](f: proc (a: T): bool {.closure.}; xs: seq[T]): seq[T]
-
Example:
import sugar doAssert dropWhile((x:int) -> bool => x<3,@[1,2,3,4,5,1,2,3]) == @[3,4,5,1,2,3] doAssert dropWhile((x:int) -> bool => x<9,@[1,2,3]) == @[] doAssert dropWhile((x:int) -> bool => x<0,@[1,2,3]) == @[1,2,3]
proc dropWhile[T](f: proc (a: T): bool {.closure.}; xs: string): string
proc elem(x: char; y: string): bool {....raises: [], tags: [].}
proc elem[T](x: T; y: seq[T]): bool
proc elemIndices(x: char; xs: string): seq[int] {....raises: [], tags: [].}
-
Example:
doAssert elemIndices('o',"Hello World") == @[4,7]
proc elemIndices[T](x: T; xs: seq[T]): seq[int]
proc findIndex[T](x: openArray[T]; f: proc (x: T): bool {.closure.}): Option[int]
-
Example:
import options,strutils,sugar doAssert "haskell".findIndex((x:char) -> bool => x>'o') == some(2) doAssert "haskell".findIndex((x:char) -> bool => x>'t') == none(int) doAssert "Hello World!".findIndex((x:char)->bool=>x.isSpaceAscii) == some(5)
proc findList[T, S](f: proc (x: S): bool {.closure.}; x: seq[T]): Option[T]
-
Example:
import sugar,options,sequtils doAssert findList((x:int) -> bool => x>6,(1..7).toSeq) == some(7) doAssert findList((x:int) -> bool => x>12,(1..7).toSeq) == none(int)
proc head(x: string): char {....raises: [], tags: [].}
-
Example:
doAssert "abcdefg".head == 'a'
proc head[T](x: seq[T]): T
-
Return the first element of a list
Example:
doAssert @[1,2,3,4].head == 1 doAssert @["abc","def","ghi","jkl"].head == "abc" doAssert @[1.1,2.2,3.3,4.4].head == 1.1
proc init(x: string): string {....raises: [], tags: [].}
-
Return all elements of a list except the last
Example:
doAssert "abcdefg".init == "abcdef"
proc init[T](x: seq[T]): seq[T]
-
Return all elements of a list except the last
Example:
doAssert @[1,2,3].init == @[1,2] doAssert @["abc","def","ghi"].init == @["abc","def"] doAssert @[1.1,2.2,3.3].init == @[1.1,2.2]
proc inits(x: string): seq[string] {....raises: [], tags: [].}
-
Example:
doAssert inits("abc") == @["","a","ab","abc"]
proc inits[T](x: seq[T]): seq[seq[T]]
-
Example:
doAssert inits(@[1,2,3]) == @[@[], @[1], @[1, 2], @[1, 2, 3]]
proc intersperse(x: char; s: string): string {....raises: [], tags: [].}
-
Example:
doAssert intersperse(',', "1234") == "1,2,3,4" doAssert intersperse(' ', "ABCD") == "A B C D"
proc intersperse[T](s: T; x: seq[T]): seq[T]
-
Example:
doAssert intersperse(1.1,@[2.2,3.3,4.4,5.5]) == @[2.2,1.1,3.3,1.1,4.4,1.1,5.5] doAssert intersperse(1,@[2,3,4,5]) == @[2,1,3,1,4,1,5]
proc isInfixOf(x, y: string): bool {....raises: [], tags: [].}
-
Whether y includes x That in other words, y is a longer number of characters than x. Note that the order of the arguments is reversed from contains in strutils
Example:
doAssert isInfixOf("Haskell", "I really like Haskell.") == true doAssert isInfixOf("lal", "I really like Haskell.") == false
proc isInfixOf[T](x, y: seq[T]): bool
-
Example:
doAssert isInfixOf(@[1,2,3],@[4,5,6,1,2,3,4,5]) == true doAssert isInfixOf(@[1,2],@[4,5,6,1,3,2,4,5]) == false
proc isPrefixOf(x: string; y: string): bool {....raises: [], tags: [].}
-
Example:
doAssert isPrefixOf("Hello","Hello World!") == true doAssert isPrefixOf("Hello","Wello Horld!") == false
proc isPrefixOf[T](x: seq[T]; y: seq[T]): bool
-
Example:
doAssert isPrefixOf(@[1,2],@[1,2,3,4]) == true doAssert isPrefixOf(@[2],@[1,2,3,4]) == false
proc isSuffixOf(x: string; y: string): bool {....raises: [], tags: [].}
-
Example:
doAssert isSuffixOf("ld!","Hello World!") == true doAssert isSuffixOf("World","Hello World!") == false
proc isSuffixOf[T](x: seq[T]; y: seq[T]): bool
-
Example:
doAssert isSuffixOf(@[1,2,3],@[1,2,3,4,5,1,2,3]) == true
proc iterate[T](number: int; f: proc (b: T): T {.closure.}; start: T): seq[T]
proc last(x: string): char {....raises: [], tags: [].}
-
Example:
doAssert "abcdefg".last == 'g'
proc last[T](x: seq[T]): T
-
Return the last element of a list
Example:
doAssert @[1,2,3,4].last == 4 doAssert @["abc","def","ghi","jkl"].last == "jkl" doAssert @[1.1,2.2,3.3,4.4].last == 4.4
proc lookup[S, T](x: T; y: seq[(T, S)]): Option[S]
proc notElem(x: char; y: string): bool {....raises: [], tags: [].}
proc notElem[T](x: T; y: seq[T]): bool
proc null(x: string): bool {....raises: [], tags: [].}
-
Example:
var a: seq[int] = @[] doAssert not(@[1,2,3].null) doAssert a.null doAssert "".null
proc null[T](x: seq[T]): bool
-
Example:
var a: seq[int] = @[] doAssert not(@[1,2,3].null) doAssert a.null doAssert "".null
proc partition[S, T](f: proc (x: S): bool {.closure.}; x: seq[T]): (seq[T], seq[T])
-
Example:
import sugar,sequtils doAssert partition((x:int)->bool=>x mod 2==0 ,(0..10).toSeq) == (@[0, 2, 4, 6, 8, 10], @[1, 3, 5, 7, 9])
proc permutations(a: string; n: int = a.len): seq[seq[char]] {....raises: [], tags: [].}
-
Example:
doAssert "aiu".permutations == @[ @['a', 'i', 'u'], @['a', 'u', 'i'], @['i', 'a', 'u'], @['i', 'u', 'a'], @['u', 'a', 'i'], @['u', 'i', 'a'] ]
proc permutations[T](a: seq[T]; n: int = a.len): seq[seq[T]]
-
Example:
doAssert @[1,2,3].permutations == @[ @[1, 2, 3], @[1, 3, 2], @[2, 1, 3],@[2, 3, 1], @[3, 1, 2], @[3, 2, 1] ]
proc replicate[T](a: int; b: seq[T]): seq[seq[T]]
proc replicate[T](a: int; b: T): seq[T]
proc scanl(f: proc (a, b: float): float {.closure.}; start: float; xs: seq[float]): seq[float] {....raises: [], tags: [].}
proc scanl(f: proc (a, b: int): int {.closure.}; start: int; xs: seq[int]): seq[ int] {....raises: [], tags: [].}
-
Example:
import sugar doAssert scanl((a,b:int)->int=>a+b,0,@[1,2,3,4]) == @[0,1,3,6,10] doAssert scanl((a,b:int)->int=>a-b,100,@[1,2,3,4]) == @[100,99,97,94,90]
proc scanl(f: proc (a, b: string): string {.closure.}; start: string; xs: seq[string]): seq[string] {....raises: [], tags: [].}
-
Example:
import sugar doAssert scanl((a,b:string)->string=>b&a,"foo",@["12","34","56"]) == @["foo", "12foo", "3412foo", "563412foo"]
proc scanl(f: proc (a: string; b: char): string {.closure.}; start: string; xs: seq[char]): seq[string] {....raises: [], tags: [].}
-
Example:
import sugar doAssert scanl((a:string,b:char)->string=>b&a,"foo",@['a', 'b', 'c', 'd']) == @["foo", "afoo", "bafoo", "cbafoo", "dcbafoo"]
proc scanl1(f: proc (a, b: float): float {.closure.}; xs: seq[float]): seq[float] {. ...raises: [], tags: [].}
proc scanl1(f: proc (a, b: int): int {.closure.}; xs: seq[int]): seq[int] {. ...raises: [], tags: [].}
proc scanr(f: proc (a, b: float): float {.closure.}; start: float; xs: seq[float]): seq[float] {....raises: [], tags: [].}
proc scanr(f: proc (a, b: int): int {.closure.}; start: int; xs: seq[int]): seq[ int] {....raises: [], tags: [].}
-
Example:
import sugar doAssert scanr((a,b:int)->int=>a-b,100,@[1,2,3,4]) == @[98,-97,99,-96,100]
proc scanr(f: proc (a, b: string): string {.closure.}; start: string; xs: seq[string]): seq[string] {....raises: [], tags: [].}
proc scanr(f: proc (a: char; b: string): string {.closure.}; start: string; xs: seq[char]): seq[string] {....raises: [], tags: [].}
-
Example:
import sugar doAssert scanr((a:char,b:string)->string=>a&b,"foo" , @['a', 'b', 'c', 'd']) == @["abcdfoo", "bcdfoo", "cdfoo", "dfoo", "foo"]
proc scanr1(f: proc (a, b: float): float {.closure.}; start: float; xs: seq[float]): seq[float] {....raises: [], tags: [].}
proc scanr1(f: proc (a, b: int): int {.closure.}; xs: seq[int]): seq[int] {. ...raises: [], tags: [].}
proc singleton[T](x: T): seq[T]
-
Example:
doAssert singleton(3) == @[3] doAssert singleton("abc") == @["abc"] doAssert singleton(1.1) == @[1.1] doAssert singleton('c') == @['c']
proc span[T](f: proc (a: T): bool; xs: seq[T]): (seq[T], seq[T])
-
Example:
import sugar doAssert span((x:int)->bool=>x<3 ,@[1,2,3,4,1,2,3,4]) == (@[1, 2], @[3, 4, 1, 2, 3, 4]) doAssert span((x:int)->bool=>x<9 ,@[1,2,3]) == (@[1, 2, 3], @[]) doAssert span((x:int)->bool=>x<0 ,@[1,2,3]) == (@[], @[1, 2, 3])
proc span[T](f: proc (a: T): bool; xs: string): (string, string)
-
Example:
import sugar doAssert span((x:char)->bool=>x<'d' ,"abcdefg") == ("abc", "defg")
proc splitAt(n: Positive; xs: string): (string, string) {....raises: [], tags: [].}
-
Example:
doAssert splitAt(3,"12345678") == ("123","45678") doAssert splitAt(3,"123") == ("123","")
proc splitAt[T](n: Positive; xs: seq[T]): (seq[T], seq[T])
-
Example:
doAssert splitAt(3,@[1,2,3,4,5]) == (@[1,2,3],@[4,5])
proc stripPrefix(s: string; xs: string): Option[string] {....raises: [], tags: [].}
-
Example:
import options doAssert stripPrefix("foo","foobar") == some("bar") doAssert stripPrefix("foo","barfoo") == none(string)
proc stripPrefix[T](s: seq[T]; xs: seq[T]): Option[seq[T]]
-
Example:
import options doAssert stripPrefix(@[1,2],@[1,2,3,4,5]) == some(@[3,4,5]) doAssert stripPrefix(@[1,2],@[3,4,5]) == none(seq[int])
proc subsequences(s: string): seq[string] {....raises: [], tags: [].}
-
Example:
doAssert "123".subsequences == @[" ", "3", "2", "23", "1", "13", "12", "123"]
proc tail(x: string): string {....raises: [], tags: [].}
-
Example:
doAssert "abcdefg".tail == "bcdefg"
proc tail[T](x: seq[T]): seq[T]
-
Return all elements of a list except the first
Example:
doAssert @[1,2,3].tail == @[2,3] doAssert @["abc","def","ghi"].tail == @["def","ghi"] doAssert @[1.1,2.2,3.3].tail == @[2.2,3.3]
proc tails(x: string): seq[string] {....raises: [], tags: [].}
-
Example:
doAssert tails("abc") == @["abc","bc","c",""]
proc tails[T](x: seq[T]): seq[seq[T]]
-
Example:
doAssert tails(@[1,2,3]) == @[@[1, 2, 3], @[2, 3], @[3], @[]]
proc take(n: int; xs: string): string {....raises: [], tags: [].}
proc take[T](n: int; xs: seq[T]): seq[T]
-
Example:
doAssert take(3,@[1,2,3,4]) == @[1,2,3] doAssert take(3,@[1.1,2.2,3.3,4.4]) == @[1.1,2.2,3.3] doAssert take(3,@["123","456","789","101112"]) == @["123","456","789"]
proc takeWhile[T](f: proc (a: T): bool {.closure.}; xs: seq[T]): seq[T]
-
Example:
import sugar proc g(a:int):bool=return a<4 doAssert takeWhile(g,@[1,2,3,4,5]) == @[1,2,3] doAssert takeWhile((x:int)->bool=>x==3,@[3,3,3,4]) == @[3,3,3] doAssert takeWhile((x:string) -> bool => x.len<3,@["12","123","12345"]) == @["12"]
proc takeWhile[T](f: proc (a: T): bool {.closure.}; xs: string): string
proc transpose(arr: seq[string]): seq[string] {....raises: [], tags: [].}
-
Example:
import sequtils doAssert @["123","456"].transpose == @["14","25","36"] doAssert @["abc","def"].transpose == @["ad","be","cf"]
proc transpose[T](arr: seq[seq[T]]): seq[seq[T]]
-
Example:
import sequtils doAssert @[@[1,2,3],@[4,5,6]].transpose == @[@[1,4],@[2,5],@[3,6]] doAssert @[@[1.1,2.2,3.3],@[4.4,5.5,6.6]].transpose == @[@[1.1,4.4],@[2.2,5.5],@[3.3,6.6]] doAssert @[@['a','b','c'],@['d','e','f']].transpose == @[@['a','d'],@['b','e'],@['c','f']]
proc uncons(x: string): Option[(char, string)] {....raises: [], tags: [].}
-
Example:
import options doAssert uncons("abcde") == some(('a',"bcde"))
proc uncons[T](x: seq[T]): Option[(T, seq[T])]
-
Return the first element of a list and the rest
Example:
import options doAssert uncons(@[1,2,3]) == some((1,@[2,3])) doAssert uncons(@[1.1,2.2,3.3]) == some((1.1,@[2.2,3.3]))
Iterators
iterator cycle(x: string): char {....raises: [], tags: [].}
iterator cycle[T](x: seq[T]): T
iterator iterate[S, U](f: proc (a: S): U; x: S): U
iterator repeatList[T](x: seq[T]): seq[T]
iterator repeatList[T](x: T): T