September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
1
Task/Generator-Exponential/00META.yaml
Normal file
1
Task/Generator-Exponential/00META.yaml
Normal file
|
|
@ -0,0 +1 @@
|
|||
--- {}
|
||||
|
|
@ -0,0 +1,228 @@
|
|||
-- powers :: Gen [Int]
|
||||
on powers(n)
|
||||
script f
|
||||
on |λ|(x)
|
||||
x ^ n as integer
|
||||
end |λ|
|
||||
end script
|
||||
fmapGen(f, enumFrom(0))
|
||||
end powers
|
||||
|
||||
|
||||
-- TEST ---------------------------------------------------
|
||||
on run
|
||||
take(10, ¬
|
||||
drop(20, ¬
|
||||
differenceGen(powers(2), powers(3))))
|
||||
|
||||
--> {529, 576, 625, 676, 784, 841, 900, 961, 1024, 1089}
|
||||
end run
|
||||
|
||||
|
||||
-- GENERIC ------------------------------------------------
|
||||
|
||||
-- Just :: a -> Maybe a
|
||||
on Just(x)
|
||||
{type:"Maybe", Nothing:false, Just:x}
|
||||
end Just
|
||||
|
||||
-- Nothing :: Maybe a
|
||||
on Nothing()
|
||||
{type:"Maybe", Nothing:true}
|
||||
end Nothing
|
||||
|
||||
-- Tuple (,) :: a -> b -> (a, b)
|
||||
on Tuple(a, b)
|
||||
{type:"Tuple", |1|:a, |2|:b, length:2}
|
||||
end Tuple
|
||||
|
||||
|
||||
-- differenceGen :: Gen [a] -> Gen [a] -> Gen [a]
|
||||
on differenceGen(ga, gb)
|
||||
-- All values of ga except any
|
||||
-- already seen in gb.
|
||||
script
|
||||
property g : zipGen(ga, gb)
|
||||
property bs : {}
|
||||
property xy : missing value
|
||||
on |λ|()
|
||||
set xy to g's |λ|()
|
||||
if missing value is xy then
|
||||
xy
|
||||
else
|
||||
set x to |1| of xy
|
||||
set y to |2| of xy
|
||||
set bs to {y} & bs
|
||||
if bs contains x then
|
||||
|λ|() -- Next in series.
|
||||
else
|
||||
x
|
||||
end if
|
||||
end if
|
||||
end |λ|
|
||||
end script
|
||||
end differenceGen
|
||||
|
||||
|
||||
-- drop :: Int -> [a] -> [a]
|
||||
-- drop :: Int -> String -> String
|
||||
on drop(n, xs)
|
||||
set c to class of xs
|
||||
if script is not c then
|
||||
if string is not c then
|
||||
if n < length of xs then
|
||||
items (1 + n) thru -1 of xs
|
||||
else
|
||||
{}
|
||||
end if
|
||||
else
|
||||
if n < length of xs then
|
||||
text (1 + n) thru -1 of xs
|
||||
else
|
||||
""
|
||||
end if
|
||||
end if
|
||||
else
|
||||
take(n, xs) -- consumed
|
||||
return xs
|
||||
end if
|
||||
end drop
|
||||
|
||||
|
||||
-- enumFrom :: Int -> [Int]
|
||||
on enumFrom(x)
|
||||
script
|
||||
property v : missing value
|
||||
on |λ|()
|
||||
if missing value is not v then
|
||||
set v to 1 + v
|
||||
else
|
||||
set v to x
|
||||
end if
|
||||
return v
|
||||
end |λ|
|
||||
end script
|
||||
end enumFrom
|
||||
|
||||
|
||||
-- fmapGen <$> :: (a -> b) -> Gen [a] -> Gen [b]
|
||||
on fmapGen(f, gen)
|
||||
script
|
||||
property g : mReturn(f)
|
||||
on |λ|()
|
||||
set v to gen's |λ|()
|
||||
if v is missing value then
|
||||
v
|
||||
else
|
||||
g's |λ|(v)
|
||||
end if
|
||||
end |λ|
|
||||
end script
|
||||
end fmapGen
|
||||
|
||||
|
||||
-- length :: [a] -> Int
|
||||
on |length|(xs)
|
||||
set c to class of xs
|
||||
if list is c or string is c then
|
||||
length of xs
|
||||
else
|
||||
(2 ^ 29 - 1) -- (maxInt - simple proxy for non-finite)
|
||||
end if
|
||||
end |length|
|
||||
|
||||
|
||||
-- Lift 2nd class handler function into 1st class script wrapper
|
||||
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
|
||||
on mReturn(f)
|
||||
if script is class of f then
|
||||
f
|
||||
else
|
||||
script
|
||||
property |λ| : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
||||
|
||||
-- take :: Int -> [a] -> [a]
|
||||
-- take :: Int -> String -> String
|
||||
on take(n, xs)
|
||||
set c to class of xs
|
||||
if list is c then
|
||||
if 0 < n then
|
||||
items 1 thru min(n, length of xs) of xs
|
||||
else
|
||||
{}
|
||||
end if
|
||||
else if string is c then
|
||||
if 0 < n then
|
||||
text 1 thru min(n, length of xs) of xs
|
||||
else
|
||||
""
|
||||
end if
|
||||
else if script is c then
|
||||
set ys to {}
|
||||
repeat with i from 1 to n
|
||||
set v to xs's |λ|()
|
||||
if missing value is v then
|
||||
return ys
|
||||
else
|
||||
set end of ys to v
|
||||
end if
|
||||
end repeat
|
||||
return ys
|
||||
else
|
||||
missing value
|
||||
end if
|
||||
end take
|
||||
|
||||
|
||||
-- uncons :: [a] -> Maybe (a, [a])
|
||||
on uncons(xs)
|
||||
set lng to |length|(xs)
|
||||
if 0 = lng then
|
||||
Nothing()
|
||||
else
|
||||
if (2 ^ 29 - 1) as integer > lng then
|
||||
if class of xs is string then
|
||||
set cs to text items of xs
|
||||
Just(Tuple(item 1 of cs, rest of cs))
|
||||
else
|
||||
Just(Tuple(item 1 of xs, rest of xs))
|
||||
end if
|
||||
else
|
||||
set nxt to take(1, xs)
|
||||
if {} is nxt then
|
||||
Nothing()
|
||||
else
|
||||
Just(Tuple(item 1 of nxt, xs))
|
||||
end if
|
||||
end if
|
||||
end if
|
||||
end uncons
|
||||
|
||||
|
||||
-- zipGen :: Gen [a] -> Gen [b] -> Gen [(a, b)]
|
||||
on zipGen(ga, gb)
|
||||
script
|
||||
property ma : missing value
|
||||
property mb : missing value
|
||||
on |λ|()
|
||||
if missing value is ma then
|
||||
set ma to uncons(ga)
|
||||
set mb to uncons(gb)
|
||||
end if
|
||||
if Nothing of ma or Nothing of mb then
|
||||
missing value
|
||||
else
|
||||
set ta to Just of ma
|
||||
set tb to Just of mb
|
||||
set x to Tuple(|1| of ta, |1| of tb)
|
||||
set ma to uncons(|2| of ta)
|
||||
set mb to uncons(|2| of tb)
|
||||
return x
|
||||
end if
|
||||
end |λ|
|
||||
end script
|
||||
end zipGen
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
(require 'generator)
|
||||
(setq lexical-binding t)
|
||||
|
||||
(iter-defun exp-gen (pow)
|
||||
(let ((i -1))
|
||||
(while
|
||||
(setq i (1+ i))
|
||||
(iter-yield (expt i pow)))))
|
||||
|
||||
(iter-defun flt-gen ()
|
||||
(let* ((g (exp-gen 2))
|
||||
(f (exp-gen 3))
|
||||
(i (iter-next g))
|
||||
(j (iter-next f)))
|
||||
(while
|
||||
(setq i (iter-next g))
|
||||
(while (> i j)
|
||||
(setq j (iter-next f)))
|
||||
(unless (= i j)
|
||||
(iter-yield i)))))
|
||||
|
||||
|
||||
(let ((g (flt-gen))
|
||||
(o 'nil))
|
||||
(dotimes (i 29)
|
||||
(setq o (iter-next g))
|
||||
(when (>= i 20)
|
||||
(print o))))
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
USING: fry kernel lists lists.lazy math math.functions
|
||||
prettyprint ;
|
||||
IN: rosetta-code.generator-exponential
|
||||
|
||||
: mth-powers-generator ( m -- lazy-list )
|
||||
[ 0 lfrom ] dip [ ^ ] curry lmap-lazy ;
|
||||
|
||||
: lmember? ( elt list -- ? )
|
||||
over '[ unswons dup _ >= ] [ drop ] until nip = ;
|
||||
|
||||
: 2-not-3-generator ( -- lazy-list )
|
||||
2 mth-powers-generator
|
||||
[ 3 mth-powers-generator lmember? not ] <lazy-filter> ;
|
||||
|
||||
10 2-not-3-generator 20 [ cdr ] times ltake list>array .
|
||||
|
|
@ -1,10 +1,16 @@
|
|||
import Data.List.Ordered
|
||||
|
||||
powers :: Int -> [Int]
|
||||
powers m = map (^ m) [0..]
|
||||
|
||||
squares :: [Int]
|
||||
squares = powers 2
|
||||
|
||||
cubes :: [Int]
|
||||
cubes = powers 3
|
||||
|
||||
foo :: [Int]
|
||||
foo = filter (not . has cubes) squares
|
||||
|
||||
main :: IO ()
|
||||
main = print $ take 10 $ drop 20 $ foo
|
||||
main = print $ take 10 $ drop 20 foo
|
||||
|
|
|
|||
155
Task/Generator-Exponential/JavaScript/generator-exponential-4.js
Normal file
155
Task/Generator-Exponential/JavaScript/generator-exponential-4.js
Normal file
|
|
@ -0,0 +1,155 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
// main :: IO()
|
||||
const main = () => {
|
||||
|
||||
// powers :: Gen [Int]
|
||||
const powers = n =>
|
||||
fmapGen(
|
||||
x => Math.pow(x, n),
|
||||
enumFrom(0)
|
||||
);
|
||||
|
||||
// xs :: [Int]
|
||||
const xs = take(10, drop(20,
|
||||
differenceGen(
|
||||
powers(2),
|
||||
powers(3)
|
||||
)
|
||||
));
|
||||
|
||||
console.log(xs);
|
||||
// -> [529,576,625,676,784,841,900,961,1024,1089]
|
||||
};
|
||||
|
||||
|
||||
// GENERIC FUNCTIONS ----------------------------------
|
||||
|
||||
// Just :: a -> Maybe a
|
||||
const Just = x => ({
|
||||
type: 'Maybe',
|
||||
Nothing: false,
|
||||
Just: x
|
||||
});
|
||||
|
||||
// Nothing :: Maybe a
|
||||
const Nothing = () => ({
|
||||
type: 'Maybe',
|
||||
Nothing: true,
|
||||
});
|
||||
|
||||
// Tuple (,) :: a -> b -> (a, b)
|
||||
const Tuple = (a, b) => ({
|
||||
type: 'Tuple',
|
||||
'0': a,
|
||||
'1': b,
|
||||
length: 2
|
||||
});
|
||||
|
||||
// differenceGen :: Gen [a] -> Gen [a] -> Gen [a]
|
||||
function* differenceGen(ga, gb) {
|
||||
// All values of generator stream a except any
|
||||
// already seen in generator stream b.
|
||||
const
|
||||
stream = zipGen(ga, gb),
|
||||
sb = new Set([]);
|
||||
let xy = take(1, stream);
|
||||
while (0 < xy.length) {
|
||||
const [x, y] = Array.from(xy[0]);
|
||||
sb.add(y);
|
||||
if (!sb.has(x)) yield x;
|
||||
xy = take(1, stream);
|
||||
}
|
||||
};
|
||||
|
||||
// drop :: Int -> [a] -> [a]
|
||||
// drop :: Int -> Generator [a] -> Generator [a]
|
||||
// drop :: Int -> String -> String
|
||||
const drop = (n, xs) =>
|
||||
Infinity > length(xs) ? (
|
||||
xs.slice(n)
|
||||
) : (take(n, xs), xs);
|
||||
|
||||
// enumFrom :: Enum a => a -> [a]
|
||||
function* enumFrom(x) {
|
||||
let v = x;
|
||||
while (true) {
|
||||
yield v;
|
||||
v = 1 + v;
|
||||
}
|
||||
}
|
||||
|
||||
// fmapGen <$> :: (a -> b) -> Gen [a] -> Gen [b]
|
||||
function* fmapGen(f, gen) {
|
||||
let v = take(1, gen);
|
||||
while (0 < v.length) {
|
||||
yield(f(v[0]))
|
||||
v = take(1, gen)
|
||||
}
|
||||
}
|
||||
|
||||
// fst :: (a, b) -> a
|
||||
const fst = tpl => tpl[0];
|
||||
|
||||
// Returns Infinity over objects without finite length.
|
||||
// This enables zip and zipWith to choose the shorter
|
||||
// argument when one is non-finite, like cycle, repeat etc
|
||||
|
||||
// length :: [a] -> Int
|
||||
const length = xs =>
|
||||
(Array.isArray(xs) || 'string' === typeof xs) ? (
|
||||
xs.length
|
||||
) : Infinity;
|
||||
|
||||
// snd :: (a, b) -> b
|
||||
const snd = tpl => tpl[1];
|
||||
|
||||
// take :: Int -> [a] -> [a]
|
||||
// take :: Int -> String -> String
|
||||
const take = (n, xs) =>
|
||||
'GeneratorFunction' !== xs.constructor.constructor.name ? (
|
||||
xs.slice(0, n)
|
||||
) : [].concat.apply([], Array.from({
|
||||
length: n
|
||||
}, () => {
|
||||
const x = xs.next();
|
||||
return x.done ? [] : [x.value];
|
||||
}));
|
||||
|
||||
// uncons :: [a] -> Maybe (a, [a])
|
||||
const uncons = xs => {
|
||||
const lng = length(xs);
|
||||
return (0 < lng) ? (
|
||||
lng < Infinity ? (
|
||||
Just(Tuple(xs[0], xs.slice(1))) // Finite list
|
||||
) : (() => {
|
||||
const nxt = take(1, xs);
|
||||
return 0 < nxt.length ? (
|
||||
Just(Tuple(nxt[0], xs))
|
||||
) : Nothing();
|
||||
})() // Lazy generator
|
||||
) : Nothing();
|
||||
};
|
||||
|
||||
// zipGen :: Gen [a] -> Gen [b] -> Gen [(a, b)]
|
||||
const zipGen = (ga, gb) => {
|
||||
function* go(ma, mb) {
|
||||
let
|
||||
a = ma,
|
||||
b = mb;
|
||||
while (!a.Nothing && !b.Nothing) {
|
||||
let
|
||||
ta = a.Just,
|
||||
tb = b.Just
|
||||
yield(Tuple(fst(ta), fst(tb)));
|
||||
a = uncons(snd(ta));
|
||||
b = uncons(snd(tb));
|
||||
}
|
||||
}
|
||||
return go(uncons(ga), uncons(gb));
|
||||
};
|
||||
|
||||
// MAIN ---
|
||||
return main();
|
||||
})();
|
||||
82
Task/Generator-Exponential/Python/generator-exponential-2.py
Normal file
82
Task/Generator-Exponential/Python/generator-exponential-2.py
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
'''Exponentials as generators'''
|
||||
|
||||
from itertools import count, islice
|
||||
|
||||
|
||||
# powers :: Gen [Int]
|
||||
def powers(n):
|
||||
'''A non-finite succession of integers,
|
||||
starting at zero,
|
||||
raised to the nth power.'''
|
||||
|
||||
def f(x):
|
||||
return pow(x, n)
|
||||
|
||||
return map(f, count(0))
|
||||
|
||||
|
||||
# main :: IO ()
|
||||
def main():
|
||||
'''Taking the difference between two derived generators.'''
|
||||
print(
|
||||
take(10)(
|
||||
drop(20)(
|
||||
differenceGen(powers(2))(
|
||||
powers(3)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
# GENERIC -------------------------------------------------
|
||||
|
||||
|
||||
# differenceGen :: Gen [a] -> Gen [a] -> Gen [a]
|
||||
def differenceGen(ga):
|
||||
'''All values of ga except any
|
||||
already seen in gb.'''
|
||||
def go(a, b):
|
||||
stream = zip(a, b)
|
||||
bs = set([])
|
||||
while True:
|
||||
xy = next(stream, None)
|
||||
if None is not xy:
|
||||
x, y = xy
|
||||
bs.add(y)
|
||||
if x not in bs:
|
||||
yield x
|
||||
else:
|
||||
return
|
||||
return lambda gb: go(ga, gb)
|
||||
|
||||
|
||||
# drop :: Int -> [a] -> [a]
|
||||
# drop :: Int -> String -> String
|
||||
def drop(n):
|
||||
'''The sublist of xs beginning at
|
||||
(zero-based) index n.'''
|
||||
def go(xs):
|
||||
if isinstance(xs, list):
|
||||
return xs[n:]
|
||||
else:
|
||||
take(n)(xs)
|
||||
return xs
|
||||
return lambda xs: go(xs)
|
||||
|
||||
|
||||
# take :: Int -> [a] -> [a]
|
||||
# take :: Int -> String -> String
|
||||
def take(n):
|
||||
'''The prefix of xs of length n,
|
||||
or xs itself if n > length xs.'''
|
||||
return lambda xs: (
|
||||
xs[0:n]
|
||||
if isinstance(xs, list)
|
||||
else list(islice(xs, n))
|
||||
)
|
||||
|
||||
|
||||
# MAIN ---
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
39
Task/Generator-Exponential/Rust/generator-exponential.rust
Normal file
39
Task/Generator-Exponential/Rust/generator-exponential.rust
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
use std::cmp::Ordering;
|
||||
use std::iter::Peekable;
|
||||
|
||||
fn powers(m: u32) -> impl Iterator<Item = u64> {
|
||||
(0u64..).map(move |x| x.pow(m))
|
||||
}
|
||||
|
||||
fn noncubic_squares() -> impl Iterator<Item = u64> {
|
||||
NoncubicSquares {
|
||||
squares: powers(2).peekable(),
|
||||
cubes: powers(3).peekable(),
|
||||
}
|
||||
}
|
||||
|
||||
struct NoncubicSquares<T: Iterator<Item = u64>, U: Iterator<Item = u64>> {
|
||||
squares: Peekable<T>,
|
||||
cubes: Peekable<U>,
|
||||
}
|
||||
|
||||
impl<T: Iterator<Item = u64>, U: Iterator<Item = u64>> Iterator for NoncubicSquares<T, U> {
|
||||
type Item = u64;
|
||||
fn next(&mut self) -> Option<u64> {
|
||||
loop {
|
||||
match self.squares.peek()?.cmp(self.cubes.peek()?) {
|
||||
Ordering::Equal => self.squares.next(),
|
||||
Ordering::Greater => self.cubes.next(),
|
||||
Ordering::Less => return self.squares.next(),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
noncubic_squares()
|
||||
.skip(20)
|
||||
.take(10)
|
||||
.for_each(|x| print!("{} ", x));
|
||||
println!();
|
||||
}
|
||||
44
Task/Generator-Exponential/VBA/generator-exponential.vba
Normal file
44
Task/Generator-Exponential/VBA/generator-exponential.vba
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
Public lastsquare As Long
|
||||
Public nextsquare As Long
|
||||
Public lastcube As Long
|
||||
Public midcube As Long
|
||||
Public nextcube As Long
|
||||
Private Sub init()
|
||||
lastsquare = 1
|
||||
nextsquare = -1
|
||||
lastcube = -1
|
||||
midcube = 0
|
||||
nextcube = 1
|
||||
End Sub
|
||||
|
||||
Private Function squares() As Long
|
||||
lastsquare = lastsquare + nextsquare
|
||||
nextsquare = nextsquare + 2
|
||||
squares = lastsquare
|
||||
End Function
|
||||
|
||||
Private Function cubes() As Long
|
||||
lastcube = lastcube + nextcube
|
||||
nextcube = nextcube + midcube
|
||||
midcube = midcube + 6
|
||||
cubes = lastcube
|
||||
End Function
|
||||
|
||||
Public Sub main()
|
||||
init
|
||||
cube = cubes
|
||||
For i = 1 To 30
|
||||
Do While True
|
||||
square = squares
|
||||
Do While cube < square
|
||||
cube = cubes
|
||||
Loop
|
||||
If square <> cube Then
|
||||
Exit Do
|
||||
End If
|
||||
Loop
|
||||
If i > 20 Then
|
||||
Debug.Print square;
|
||||
End If
|
||||
Next i
|
||||
End Sub
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
Module Program
|
||||
Iterator Function IntegerPowers(exp As Integer) As IEnumerable(Of Integer)
|
||||
Dim i As Integer = 0
|
||||
Do
|
||||
Yield CInt(Math.Pow(i, exp))
|
||||
i += 1
|
||||
Loop
|
||||
End Function
|
||||
|
||||
Function Squares() As IEnumerable(Of Integer)
|
||||
Return IntegerPowers(2)
|
||||
End Function
|
||||
|
||||
Function Cubes() As IEnumerable(Of Integer)
|
||||
Return IntegerPowers(3)
|
||||
End Function
|
||||
|
||||
Iterator Function SquaresWithoutCubes() As IEnumerable(Of Integer)
|
||||
Dim cubeSequence = Cubes().GetEnumerator()
|
||||
Dim nextGreaterOrEqualCube As Integer = 0
|
||||
For Each curSquare In Squares()
|
||||
Do While nextGreaterOrEqualCube < curSquare
|
||||
cubeSequence.MoveNext()
|
||||
nextGreaterOrEqualCube = cubeSequence.Current
|
||||
Loop
|
||||
If nextGreaterOrEqualCube <> curSquare Then Yield curSquare
|
||||
Next
|
||||
End Function
|
||||
|
||||
Sub Main()
|
||||
For Each x In From i In SquaresWithoutCubes() Skip 20 Take 10
|
||||
Console.WriteLine(x)
|
||||
Next
|
||||
End Sub
|
||||
End Module
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
Function SquaresWithoutCubesLinq() As IEnumerable(Of Integer)
|
||||
Return Squares().Where(Function(s) s <> Cubes().First(Function(c) c >= s))
|
||||
End Function
|
||||
Loading…
Add table
Add a link
Reference in a new issue