F# filtering list - f#

How would I filter out a list by the index ?
List: [400; 5; 401; 6; 403; 7]
Filtered List: [5; 6; 7;]
I want to filter out the odd index numbers. So I could compare the values and then print out the largest value.

A simple and straight forward approach would be to recurse through the list and pick every second element:
let getOddIndexed list =
let rec aux acc xs =
match xs with
| _::x::tail -> aux (x::acc) tail
| _ -> acc |> List.rev
aux [] list

In the specific case you asked about (keeping the odd index numbers and dropping the even ones), John Reynolds' answer will work. But in the general case where your index-based filter is more complicated, you'd want to use Seq.indexed, which turns any list of items into a list of (index, item) pairs. E.g.:
["apple"; "banana"; "cherry"] |> Seq.indexed |> List.ofSeq
// Produces [(0, "apple"); (1, "banana"); (2, "cherry")]
With this approach, you would then use Seq.filter to do the filtering you want, then turn the sequence back into a list at the end:
let keepOdd (idx, item) =
// A more complicated filter might use the item parameter too
idx % 2 <> 0
let input = [400; 5; 401; 6; 403; 7]
input |> Seq.indexed |> Seq.filter keepOdd |> List.ofSeq

Here is another solution using foldBack
let folder x (index,lst) =
if index % 2 = 0
then (index+1, x::lst)
else (index+1, lst)
let list = snd (List.foldBack folder [400; 5; 401; 6; 403; 7] (0,[]))

Related

how to calculate the max by column in a list of lists, with F#

With the following data (random strings):
let data =
[
["shi"; "cjwocij"; "cjwijo"]
["abs"; "djw"; "djwjdwojdow"]
["djido"; "dkwpkw"; "dpfkpeoep"]
]
I would like to calculate the max length by column.
I can transform the data into a list of lists of lengths:
data |> List.map (fun r -> r |> List.map (fun x -> x.Length))
index value
0 [ 3; 7; 6 ]
1 [ 3; 3; 11 ]
2 [ 5; 6; 9 ]
but ultimately I'd like to reduce it to:
[5; 7; 11]
by taking the max of each column
I thought I could do a fold and take the first row as a starting point, but it looks like it'll be quite convoluted.
For context: it's for a function that will take a grid and display it with proper spacing / formatting; all rows must have the same number of elements.
You need List.transpose. This turns rows into columns and vice versa.
data
|> List.map (fun r -> r |> List.map (fun x -> x.Length))
|> List.transpose
|> List.map List.max
val data : string list list =
[["shi"; "cjwocij"; "cjwijo"]; ["abs"; "djw"; "djwjdwojdow"];
["djido"; "dkwpkw"; "dpfkpeoep"]]
val it : int list = [5; 7; 11]
Here is a quick explicit version of transpose that might help (or hinder?) you grokking this:
let transpose lists =
[0 .. (List.length (List.head lists)) - 1]
|> List.map (fun col -> List.map (fun row -> List.item col row) lists )
This is the recursive version for completeness but I think the above is clearer if you are new to F#.
let rec transpose = function
| (_::_)::_ as l -> List.map List.head l :: transpose (List.map List.tail l)
| _ -> []
Update: I answered this in a comment but for others wondering over the second recursive solution:
The (_::_) ensures that a list is not empty. This is the same as (h::t) but we don't need to use h and t and so put the don't care place holder _ instead.
The second :: makes sure that the list of lists (listoflists) is non-empty or that (h::t)::c = (head of listoflists)::tail of listoflists so h::t is the first row, and c is all the other rows.
(_::_)::_ <=> (headRow0::tailRow0)::tailRows

How to copy elements in a list different number of times?

If I have a list consisting of elements [5;10;15], how can I get an output array in which the number of times element x from the input list is repeated x/5 times, or using any other integral expression to specify the number of repetitions?
So like for this list, the expected output should be [5; 10; 10; 15; 15; 15]
First, transform every element of the input list into a list with that many elements. To transform every element of a list, use List.map. To create a list with a given number of elements, use List.replicate:
let f lst =
lst
|> List.map (fun x -> List.replicate (x/5) x)
Let's test that:
> f [5; 10]
[ [5]; [10; 10] ]
And then, all that's left to do is concatenate all those lists into a big list using List.concat:
let f lst =
lst
|> List.map (fun x -> List.replicate (x/5) x)
|> List.concat
Or you can fuse map and concat together in collect:
let f lst =
lst
|> List.collect (fun x -> List.replicate (x/5) x)
You can define the following function returning a sequence using a sequence expression with nested for loops:
let repeatedSeq (s, itemCount) =
seq { for item in s do for i in 1..itemCount(item) -> item }
And then do:
let list = [5; 10; 15]
let repeatedList = repeatedSeq(list, fun i -> i/5) |> Seq.toList
Which results in [5; 10; 10; 15; 15; 15] as required. This approach avoids materializing any intermediate sequences as lists or arrays.
If you want your final result to be an array do |> Seq.toArray instead.
Demo fiddle here.

F# pattern filtering

Can someone please give me advice on how to filter out a list of lists when it contains a certain number. For example, if the sub-lists contain 2 then I want the third value.
let p = [ [0;2;1]; [7;2;5]; [8;2; 10]; [44; 33; 9]]
//Filtered List: [1;5;10]
let q = p |> List.filter(fun((x,y):int List) (List.item 2 x) = 1, (List.item 3 y))
Above is my code so far. I know its wrong but can't seem to figure it out in code.
s952163's answer is correct, however, List.choose would be better.
p
|> List.choose (fun list ->
if List.contains 2 list then
Some list.[2]
else
None
)
With the solution above you only iterate through the list once.
I think you can just go by the way you described the problem:
p
|> List.filter (List.contains 2) //first filter out lists with 2 in it
|> List.map (fun x -> x.[2]) //get the third element, this is the same as List.item 2
//val it : int list = [1; 5; 10]

F# divide sequence up in blocks [duplicate]

I'm trying to learn F# by rewriting some C# algorithms I have into idiomatic F#.
One of the first functions I'm trying to rewrite is a batchesOf where:
[1..17] |> batchesOf 5
Which would split the sequence into batches with a max of five in each, i.e:
[[1; 2; 3; 4; 5]; [6; 7; 8; 9; 10]; [11; 12; 13; 14; 15]; [16; 17]]
My first attempt at doing this is kind of ugly where I've resorted to using a mutable ref object after running into errors trying to use mutable type inside the closure. Using ref is particularly unpleasant since to dereference it you have to use the ! operator which when inside a condition expression can be counter intuitive to some devs who will read it as logical not. Another problem I ran into is where Seq.skip and Seq.take are not like their Linq aliases in that they will throw an error if size exceeds the size of the sequence.
let batchesOf size (sequence: _ seq) : _ list seq =
seq {
let s = ref sequence
while not (!s |> Seq.isEmpty) do
yield !s |> Seq.truncate size |> List.ofSeq
s := System.Linq.Enumerable.Skip(!s, size)
}
Anyway what would be the most elegant/idiomatic way to rewrite this in F#? Keeping the original behaviour but preferably without the ref mutable variable.
Implementing this function using the seq<_> type idiomatically is difficult - the type is inherently mutable, so there is no simple nice functional way. Your version is quite inefficient, because it uses Skip repeatedly on the sequence. A better imperative option would be to use GetEnumerator and just iterate over elements using IEnumerator. You can find various imperative options in this snippet: http://fssnip.net/1o
If you're learning F#, then it is better to try writing the function using F# list type. This way, you can use idiomatic functional style. Then you can write batchesOf using pattern matching with recursion and accumulator argument like this:
let batchesOf size input =
// Inner function that does the actual work.
// 'input' is the remaining part of the list, 'num' is the number of elements
// in a current batch, which is stored in 'batch'. Finally, 'acc' is a list of
// batches (in a reverse order)
let rec loop input num batch acc =
match input with
| [] ->
// We've reached the end - add current batch to the list of all
// batches if it is not empty and return batch (in the right order)
if batch <> [] then (List.rev batch)::acc else acc
|> List.rev
| x::xs when num = size - 1 ->
// We've reached the end of the batch - add the last element
// and add batch to the list of batches.
loop xs 0 [] ((List.rev (x::batch))::acc)
| x::xs ->
// Take one element from the input and add it to the current batch
loop xs (num + 1) (x::batch) acc
loop input 0 [] []
As a footnote, the imperative version can be made a bit nicer using computation expression for working with IEnumerator, but that's not standard and it is quite advanced trick (for example, see http://fssnip.net/37).
A friend asked me this a while back. Here's a recycled answer. This works and is pure:
let batchesOf n =
Seq.mapi (fun i v -> i / n, v) >>
Seq.groupBy fst >>
Seq.map snd >>
Seq.map (Seq.map snd)
Or an impure version:
let batchesOf n =
let i = ref -1
Seq.groupBy (fun _ -> i := !i + 1; !i / n) >> Seq.map snd
These produce a seq<seq<'a>>. If you really must have an 'a list list as in your sample then just add ... |> Seq.map (List.ofSeq) |> List.ofSeq as in:
> [1..17] |> batchesOf 5 |> Seq.map (List.ofSeq) |> List.ofSeq;;
val it : int list list = [[1; 2; 3; 4; 5]; [6; 7; 8; 9; 10]; [11; 12; 13; 14; 15]; [16; 17]]
Hope that helps!
This can be done without recursion if you want
[0..20]
|> Seq.mapi (fun i elem -> (i/size),elem)
|> Seq.groupBy (fun (a,_) -> a)
|> Seq.map (fun (_,se) -> se |> Seq.map (snd));;
val it : seq<seq<int>> =
seq
[seq [0; 1; 2; 3; ...]; seq [5; 6; 7; 8; ...]; seq [10; 11; 12; 13; ...];
seq [15; 16; 17; 18; ...]; ...]
Depending on how you think this may be easier to understand. Tomas' solution is probably more idiomatic F# though
Hurray, we can use List.chunkBySize, Seq.chunkBySize and Array.chunkBySize in F# 4, as mentioned by Brad Collins and Scott Wlaschin.
This isn't perhaps idiomatic but it works:
let batchesOf n l =
let _, _, temp', res' = List.fold (fun (i, n, temp, res) hd ->
if i < n then
(i + 1, n, hd :: temp, res)
else
(1, i, [hd], (List.rev temp) :: res))
(0, n, [], []) l
(List.rev temp') :: res' |> List.rev
Here's a simple implementation for sequences:
let chunks size (items:seq<_>) =
use e = items.GetEnumerator()
let rec loop i acc =
seq {
if i = size then
yield (List.rev acc)
yield! loop 0 []
elif e.MoveNext() then
yield! loop (i+1) (e.Current::acc)
else
yield (List.rev acc)
}
if size = 0 then invalidArg "size" "must be greater than zero"
if Seq.isEmpty items then Seq.empty else loop 0 []
let s = Seq.init 10 id
chunks 3 s
//output: seq [[0; 1; 2]; [3; 4; 5]; [6; 7; 8]; [9]]
My method involves converting the list to an array and recursively chunking the array:
let batchesOf (sz:int) lt =
let arr = List.toArray lt
let rec bite curr =
if (curr + sz - 1 ) >= arr.Length then
[Array.toList arr.[ curr .. (arr.Length - 1)]]
else
let curr1 = curr + sz
(Array.toList (arr.[curr .. (curr + sz - 1)])) :: (bite curr1)
bite 0
batchesOf 5 [1 .. 17]
[[1; 2; 3; 4; 5]; [6; 7; 8; 9; 10]; [11; 12; 13; 14; 15]; [16; 17]]
I found this to be a quite terse solution:
let partition n (stream:seq<_>) = seq {
let enum = stream.GetEnumerator()
let rec collect n partition =
if n = 1 || not (enum.MoveNext()) then
partition
else
collect (n-1) (partition # [enum.Current])
while enum.MoveNext() do
yield collect n [enum.Current]
}
It works on a sequence and produces a sequence. The output sequence consists of lists of n elements from the input sequence.
You can solve your task with analog of Clojure partition library function below:
let partition n step coll =
let rec split ss =
seq {
yield(ss |> Seq.truncate n)
if Seq.length(ss |> Seq.truncate (step+1)) > step then
yield! split <| (ss |> Seq.skip step)
}
split coll
Being used as partition 5 5 it will provide you with sought batchesOf 5 functionality:
[1..17] |> partition 5 5;;
val it : seq<seq<int>> =
seq
[seq [1; 2; 3; 4; ...]; seq [6; 7; 8; 9; ...]; seq [11; 12; 13; 14; ...];
seq [16; 17]]
As a premium by playing with n and step you can use it for slicing overlapping batches aka sliding windows, and even apply to infinite sequences, like below:
Seq.initInfinite(fun x -> x) |> partition 4 1;;
val it : seq<seq<int>> =
seq
[seq [0; 1; 2; 3]; seq [1; 2; 3; 4]; seq [2; 3; 4; 5]; seq [3; 4; 5; 6];
...]
Consider it as a prototype only as it does many redundant evaluations on the source sequence and not likely fit for production purposes.
This version passes all my tests I could think of including ones for lazy evaluation and single sequence evaluation:
let batchIn batchLength sequence =
let padding = seq { for i in 1 .. batchLength -> None }
let wrapped = sequence |> Seq.map Some
Seq.concat [wrapped; padding]
|> Seq.windowed batchLength
|> Seq.mapi (fun i el -> (i, el))
|> Seq.filter (fun t -> fst t % batchLength = 0)
|> Seq.map snd
|> Seq.map (Seq.choose id)
|> Seq.filter (fun el -> not (Seq.isEmpty el))
I am still quite new to F# so if I'm missing anything - please do correct me, it will be greatly appreciated.

Take N elements from sequence with N different indexes in F#

I'm new to F# and looking for a function which take N*indexes and a sequence and gives me N elements. If I have N indexes it should be equal to concat Seq.nth index0, Seq.nth index1 .. Seq.nth indexN but it should only scan over indexN elements (O(N)) in the sequence and not index0+index1+...+indexN (O(N^2)).
To sum up, I'm looking for something like:
//For performance, the index-list should be ordered on input, be padding between elements instead of indexes or be ordered when entering the function
seq {10 .. 20} |> Seq.takeIndexes [0;5;10]
Result: 10,15,20
I could make this by using seq { yield... } and have a index-counter to tick when some element should be passed out but if F# offers a nice standard way I would rather use that.
Thanks :)...
Addition: I have made the following. It works but ain't pretty. Suggestions is welcomed
let seqTakeIndexes (indexes : int list) (xs : seq<int>) =
seq {
//Assume indexes is sorted
let e = xs.GetEnumerator()
let i = ref indexes
let curr = ref 0
while e.MoveNext() && not (!i).IsEmpty do
if !curr = List.head !i then
i := (!i).Tail
yield e.Current
curr := !curr + 1
}
When you want to access elements by index, then using sequences isn't as good idea. Sequences are designed to allow sequential iteration. I would convert the necessary part of the sequence to an array and then pick the elements by index:
let takeIndexes ns input =
// Take only elements that we need to access (sequence could be infinite)
let arr = input |> Seq.take (1 + Seq.max ns) |> Array.ofSeq
// Simply pick elements at the specified indices from the array
seq { for index in ns -> arr.[index] }
seq [10 .. 20] |> takeIndexes [0;5;10]
Regarding your implementation - I don't think it can be made significantly more elegant. This is a general problem when implementing functions that need to take values from multiple sources in an interleaved fashion - there is just no elegant way of writing those!
However, you can write this in a functional way using recursion like this:
let takeIndexes indices (xs:seq<int>) =
// Iterates over the list of indices recursively
let rec loop (xe:IEnumerator<_>) idx indices = seq {
let next = loop xe (idx + 1)
// If the sequence ends, then end as well
if xe.MoveNext() then
match indices with
| i::indices when idx = i ->
// We're passing the specified index
yield xe.Current
yield! next indices
| _ ->
// Keep waiting for the first index from the list
yield! next indices }
seq {
// Note: 'use' guarantees proper disposal of the source sequence
use xe = xs.GetEnumerator()
yield! loop xe 0 indices }
seq [10 .. 20] |> takeIndexes [0;5;10]
When you need to scan a sequence and accumulate results in O(n), you can always fall back to Seq.fold:
let takeIndices ind sq =
let selector (idxLeft, currIdx, results) elem =
match idxLeft with
| [] -> (idxLeft, currIdx, results)
| idx::moreIdx when idx = currIdx -> (moreIdx, currIdx+1, elem::results)
| idx::_ when idx <> currIdx -> (idxLeft, currIdx+1, results)
| idx::_ -> invalidOp "Can't get here."
let (_, _, results) = sq |> Seq.fold selector (ind, 0, [])
results |> List.rev
seq [10 .. 20] |> takeIndices [0;5;10]
The drawback of this solution is that it will enumerate the sequence to the end, even if it has accumulated all the desired elements already.
Here is my shot at this. This solution will only go as far as it needs into the sequence and returns the elements as a list.
let getIndices xs (s:_ seq) =
let enum = s.GetEnumerator()
let rec loop i acc = function
| h::t as xs ->
if enum.MoveNext() then
if i = h then
loop (i+1) (enum.Current::acc) t
else
loop (i+1) acc xs
else
raise (System.IndexOutOfRangeException())
| _ -> List.rev acc
loop 0 [] xs
[10..20]
|> getIndices [2;4;8]
// Returns [12;14;18]
The only assumption made here is that the index list you supply is sorted. The function won't work properly otherwise.
Is it a problem, that the returned result is sorted?
This algorithm will work linearly over the input sequence. Just the indices need to be sorted. If the sequence is large, but indices are not so many - it'll be fast.
Complexity is: N -> Max(indices), M -> count of indices: O(N + MlogM) in the worst case.
let seqTakeIndices indexes =
let rec gather prev idxs xs =
match idxs with
| [] -> Seq.empty
| n::ns -> seq { let left = xs |> Seq.skip (n - prev)
yield left |> Seq.head
yield! gather n ns left }
indexes |> List.sort |> gather 0
Here is a List.fold variant, but is more complex to read. I prefer the first:
let seqTakeIndices indices xs =
let gather (prev, xs, res) n =
let left = xs |> Seq.skip (n - prev)
n, left, (Seq.head left)::res
let _, _, res = indices |> List.sort |> List.fold gather (0, xs, [])
res
Appended: Still slower than your variant, but a lot faster than mine older variants. Because of not using Seq.skip that is creating new enumerators and was slowing down things a lot.
let seqTakeIndices indices (xs : seq<_>) =
let enum = xs.GetEnumerator()
enum.MoveNext() |> ignore
let rec gather prev idxs =
match idxs with
| [] -> Seq.empty
| n::ns -> seq { if [1..n-prev] |> List.forall (fun _ -> enum.MoveNext()) then
yield enum.Current
yield! gather n ns }
indices |> List.sort |> gather 0

Resources