Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1,3 +1,5 @@
|
|||
Select certain elements from an Array into a new Array in a generic way. To demonstrate, select all even numbers from an Array.
|
||||
Select certain elements from an Array into a new Array in a generic way.
|
||||
To demonstrate, select all even numbers from an Array.
|
||||
|
||||
As an option, give a second solution which filters destructively, by modifying the original Array rather than creating a new Array.
|
||||
As an option, give a second solution which filters destructively,
|
||||
by modifying the original Array rather than creating a new Array.
|
||||
|
|
|
|||
|
|
@ -1,2 +1 @@
|
|||
$ awk 'BEGIN{split("1 2 3 4 5 6 7 8 9",a);for(i in a)if(!(a[i]%2))r=r" "a[i];print r}'
|
||||
4 6 8 2
|
||||
5
Task/Filter/AWK/filter-2.awk
Normal file
5
Task/Filter/AWK/filter-2.awk
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
BEGIN {
|
||||
split("1 2 3 4 5 6 7 8 9",a);
|
||||
for(i in a) if( !(a[i]%2) ) r = r" "a[i];
|
||||
print r
|
||||
}
|
||||
1
Task/Filter/CoffeeScript/filter.coffee
Normal file
1
Task/Filter/CoffeeScript/filter.coffee
Normal file
|
|
@ -0,0 +1 @@
|
|||
[1..10].filter (x) -> not (x%2)
|
||||
|
|
@ -1,9 +1,7 @@
|
|||
import std.algorithm;
|
||||
|
||||
void main() {
|
||||
import std.algorithm: filter, equal;
|
||||
|
||||
immutable data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
||||
|
||||
auto evens = data.filter!(x => x % 2 == 0)(); // lazy
|
||||
|
||||
auto evens = data.filter!(x => x % 2 == 0); // Lazy.
|
||||
assert(evens.equal([2, 4, 6, 8, 10]));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,5 +5,4 @@ julia> filter(iseven, [1,2,3,4,5,6,7,8,9])
|
|||
6
|
||||
8
|
||||
|
||||
julia> filter(isdigit, "distance: 150 000 000")
|
||||
"150000000"
|
||||
julia>
|
||||
|
|
|
|||
11
Task/Filter/Lua/filter-1.lua
Normal file
11
Task/Filter/Lua/filter-1.lua
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
function filter(t, func)
|
||||
local ret = {}
|
||||
for i, v in ipairs(t) do
|
||||
ret[#ret+1] = func(v) and v or nil
|
||||
end
|
||||
return ret
|
||||
end
|
||||
|
||||
function even(a) return a % 2 == 0 end
|
||||
|
||||
print(unpack(filter({1, 2, 3, 4 ,5, 6, 7, 8, 9, 10}, even)))
|
||||
11
Task/Filter/Lua/filter-2.lua
Normal file
11
Task/Filter/Lua/filter-2.lua
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
function filter(t, func)
|
||||
for i, v in ipairs(t) do
|
||||
if not func(v) then table.remove(t, i) end
|
||||
end
|
||||
end
|
||||
|
||||
function even(a) return a % 2 == 0 end
|
||||
|
||||
local values = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
|
||||
filter(values, even)
|
||||
print(unpack(values))
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
function filter(t, func)
|
||||
local ret = {}
|
||||
for i, v in ipairs(t) do ret[#ret+1] = func(v) and v or nil end
|
||||
return ret
|
||||
end
|
||||
|
||||
function even(a) return a % 2 == 0 end
|
||||
|
||||
print(unpack(filter({1,2,3,4,5,6,7,8,9,10},even)))
|
||||
27
Task/Filter/Rust/filter.rust
Normal file
27
Task/Filter/Rust/filter.rust
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
fn main() {
|
||||
// Create a new vector, then use retain to filter what we want.
|
||||
// Hopefully we will see .copy_iter() or .clone_iter() soon so
|
||||
// we can write code like this:
|
||||
|
||||
/*
|
||||
let nums = range(1i32, 20i32).collect::<Vec<i32>>();
|
||||
let evens = nums.copy_iter().filter(|x| x % 2 == 0).collect::<Vec<i32>>();
|
||||
for i in evens.iter() {
|
||||
println!("{}", i)
|
||||
}
|
||||
*/
|
||||
println!("new vec filtered: ");
|
||||
let nums = range(1i32, 20i32).collect::<Vec<i32>>();
|
||||
let evens = nums.iter().map(|x| x.clone()).filter(|x| x % 2 == 0).collect::<Vec<i32>>();
|
||||
for i in evens.iter() {
|
||||
println!("{}", i)
|
||||
}
|
||||
|
||||
// Filter an already existing vector
|
||||
println!("original vec filtered: ");
|
||||
let mut nums = range(1i32, 20i32).collect::<Vec<i32>>();
|
||||
nums.retain(|x| x % 2 == 0);
|
||||
for i in nums.iter() {
|
||||
println!("{}", i)
|
||||
}
|
||||
}
|
||||
|
|
@ -13,5 +13,5 @@ let $long := for $value in $array
|
|||
return
|
||||
<result>
|
||||
<short>{$short}</short>
|
||||
<long>{$short}</long>
|
||||
<long>{$long}</long>
|
||||
</result>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue