Cav Examples

A notebook full of examples of Cav. These examples will get you started in no time, and it also serves as an API reference.

Start by placing the following line in your program:


In [1]:
using Cav

is_pos, is_neg, is_zero, is_one


In [2]:
{is_pos(1), is_pos(0), is_pos(-1), is_pos([-1,0,1])}


Out[2]:
4-element Array{Any,1}:
  true                      
 false                      
 false                      
      Bool[false,false,true]

In [3]:
{is_neg(1), is_neg(0), is_neg(-1), is_neg([-1,0,1])}


Out[3]:
4-element Array{Any,1}:
 false                      
 false                      
  true                      
      Bool[true,false,false]

In [4]:
{is_zero(1), is_zero(0), is_zero([-1,0,1])}


Out[4]:
3-element Array{Any,1}:
 false                      
  true                      
      Bool[false,true,false]

In [5]:
{is_one(1), is_one(0), is_one([-1,0,1])}


Out[5]:
3-element Array{Any,1}:
  true                      
 false                      
      Bool[false,false,true]

iterate


In [6]:
take(5, iterate(inc, 0)) |> collect


Out[6]:
5-element Array{Any,1}:
 0
 1
 2
 3
 4

mapcat


In [7]:
mapcat(inc, {[1,2,3], [4,5,6]})


Out[7]:
6-element Array{Int64,1}:
 2
 3
 4
 5
 6
 7

filter


In [8]:
it = lfilter(isodd, 1:10)
collect(it)


Out[8]:
5-element Array{Any,1}:
 1
 3
 5
 7
 9

In [9]:
it = lfilter(iseven, 1:10)
collect(it)


Out[9]:
5-element Array{Any,1}:
  2
  4
  6
  8
 10

partition


In [10]:
partition(3, 1:10)


Out[10]:
3-element Array{UnitRange{Int64},1}:
 1:3
 4:6
 7:9

In [11]:
it = partition(3, take(10, 1:10))
collect(it)


Out[11]:
3-element Array{Any,1}:
 {1,2,3}
 {4,5,6}
 {7,8,9}

partition_all


In [12]:
partition_all(3, [1:10])


Out[12]:
4-element Array{Array{Int64,1},1}:
 [1,2,3]
 [4,5,6]
 [7,8,9]
 [10]   

In [13]:
it = partition_all(3, take(10, 1:10))
collect(it)


Out[13]:
4-element Array{Any,1}:
 {1,2,3}
 {4,5,6}
 {7,8,9}
 {10}   

take


In [14]:
it = take(0, [1,2,3])
collect(it)


Out[14]:
0-element Array{Any,1}

drop


In [15]:
it = drop(0, [1,2,3])
collect(it)


Out[15]:
3-element Array{Any,1}:
 1
 2
 3

lconcat


In [16]:
it = lconcat([1,2,3], [4,5,6], [7,8,9])
collect(it)


Out[16]:
9-element Array{Any,1}:
 1
 2
 3
 4
 5
 6
 7
 8
 9

interleave


In [17]:
interleave([1,2,3])


Out[17]:
3-element Array{Int64,1}:
 1
 2
 3

In [18]:
interleave([1.,2,3], [4.,5,6])


Out[18]:
6-element Array{Float64,1}:
 1.0
 4.0
 2.0
 5.0
 3.0
 6.0

In [19]:
interleave([1.,2,3], [4.,5,6], [7.,8,9])


Out[19]:
9-element Array{Float64,1}:
 1.0
 4.0
 7.0
 2.0
 5.0
 8.0
 3.0
 6.0
 9.0

In [20]:
interleave([1, 2, 3], [4, 5])


Out[20]:
4-element Array{Int64,1}:
 1
 4
 2
 5

In [21]:
interleave([1, 2], [3, 4, 5])


Out[21]:
4-element Array{Int64,1}:
 1
 3
 2
 4

In [22]:
it = iterate(inc, 1)
interleave(it)


Out[22]:
IterateIterator{Int64}(inc,1)

In [23]:
it1 = take(3, iterate(inc, 1))
it2 = take(3, iterate(dec, -1))
collect(Int64, interleave(it1, it2))


Out[23]:
6-element Array{Int64,1}:
  1
 -1
  2
 -2
  3
 -3

In [24]:
it1 = take(3, iterate(inc, 1))
it2 = take(2, iterate(dec, -1))
collect(Int64, interleave(it1, it2))


Out[24]:
4-element Array{Int64,1}:
  1
 -1
  2
 -2

In [25]:
it1 = take(2, iterate(inc, 1))
it2 = take(3, iterate(dec, -1))
collect(Int64, interleave(it1, it2))


Out[25]:
4-element Array{Int64,1}:
  1
 -1
  2
 -2

In [26]:
it1 = iterate(inc, 1)
it2 = iterate(dec, -1)
interleaved = interleave(it1, it2)
collect(Int64, take(6, interleaved))


Out[26]:
6-element Array{Int64,1}:
  1
 -1
  2
 -2
  3
 -3

In [27]:
it = enumerate(take(5, lmap(x->2x, iterate(inc, 1))))
collect(it)


Out[27]:
5-element Array{(Int64,Any),1}:
 (1,2) 
 (2,4) 
 (3,6) 
 (4,8) 
 (5,10)

separate


In [28]:
it = interleave([1,2,3], [4,5,6])
separate(2, it)


Out[28]:
2-element Array{Array{Int64,1},1}:
 [1,2,3]
 [4,5,6]

In [29]:
it = interleave(take(3, [1,2,3]), take(3, [4,5,6]))
separate(2, it) |> (c -> map(x -> collect(Int64, x), c))


Out[29]:
2-element Array{Array{Int64,1},1}:
 [1,2,3]
 [4,5,6]

In [30]:
it = interleave(take(3, [1,2,3]), take(3, [4,5,6]))
separate(1, it) |> (c -> map(x -> collect(Int64, x), c))


Out[30]:
1-element Array{Array{Int64,1},1}:
 [1,4,2,5,3,6]

split_at


In [31]:
split_at(0, [1,2,3,4])


Out[31]:
2-element Array{Array{Int64,1},1}:
 []       
 [1,2,3,4]

In [32]:
split_at(1, [1,2,3,4])


Out[32]:
2-element Array{Array{Int64,1},1}:
 [1]    
 [2,3,4]

In [33]:
split_at(2, [1,2,3,4])


Out[33]:
2-element Array{Array{Int64,1},1}:
 [1,2]
 [3,4]

In [34]:
l, r = split_at(0, take(4, [1,2,3,4]))
(collect(l), collect(r))


Out[34]:
({},{1,2,3,4})

In [35]:
l, r = split_at(1, take(4, [1,2,3,4]))
(collect(l), collect(r))


Out[35]:
({1},{2,3,4})

In [36]:
l, r = split_at(2, take(4, [1,2,3,4]))
(collect(l), collect(r))


Out[36]:
({1,2},{3,4})