Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -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.

View file

@ -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

View 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
}

View file

@ -0,0 +1 @@
[1..10].filter (x) -> not (x%2)

View file

@ -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]));
}

View file

@ -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>

View 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)))

View 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))

View file

@ -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)))

View 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)
}
}

View file

@ -13,5 +13,5 @@ let $long := for $value in $array
return
<result>
<short>{$short}</short>
<long>{$short}</long>
<long>{$long}</long>
</result>