Skip to content

Commit

Permalink
- Extended the Array unroll-operator (»op«) to work with arrays of …
Browse files Browse the repository at this point in the history
…different sizes

Example:
	[1,2,3] »+« [1];   # => [1+1, 2+1, 3+1]
	[1,2,3] »+« [1,2]; # => [1+1, 2+2, 3+1]

new file:   scripts/Rosettacode/Partial_function_application.sf
new file:   scripts/vigenere_cipher.sf
  • Loading branch information
trizen committed Sep 10, 2015
1 parent 4ad3793 commit 3b8612a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
2 changes: 2 additions & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,7 @@ scripts/Rosettacode/Order_disjoint_list_items.sf
scripts/Rosettacode/Palindrome_detection.sf
scripts/Rosettacode/Palindrome_detection_1.sf
scripts/Rosettacode/Pangram_checker.sf
scripts/Rosettacode/Partial_function_application.sf
scripts/Rosettacode/Pascal_s_triangle.sf
scripts/Rosettacode/Permutations.sf
scripts/Rosettacode/Permutations_1.sf
Expand Down Expand Up @@ -882,6 +883,7 @@ scripts/unique_prefixes.sf
scripts/url_encoding_decoding.sf
scripts/var_ref.sf
scripts/var_scoping.sf
scripts/vigenere_cipher.sf
scripts/virtual_machine.sf
scripts/while_loop.sf
scripts/word_roots.sf
Expand Down
7 changes: 5 additions & 2 deletions lib/Sidef/Types/Array/Array.pm
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ package Sidef::Types::Array::Array {

my @array;
if (defined $arg) {
foreach my $i (0 .. $#{$self}) {
push @array, $self->[$i]->get_value->$operator($arg->[$i]->get_value);
my $argc = @{$arg};
my $selfc = @{$self};
my $max = $argc > $selfc ? $argc - 1 : $selfc - 1;
foreach my $i (0 .. $max) {
push @array, $self->[$i % $selfc]->get_value->$operator($arg->[$i % $argc]->get_value);
}
}
else {
Expand Down
25 changes: 25 additions & 0 deletions scripts/Rosettacode/Partial_function_application.sf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/ruby

#
## http://rosettacode.org/wiki/Partial_function_application
#

func fs(f) {
closure func(*args) {
args.map {f(_)}
}
}

func double(n) { n * 2 };
func square(n) { n ** 2 };

var fs_double = fs(double);
var fs_square = fs(square);

var s = (0 .. 3);
say "fs_double(#{s}): #{fs_double(s...)}";
say "fs_square(#{s}): #{fs_square(s...)}";

s = [2, 4, 6, 8];
say "fs_double(#{s}): #{fs_double(s...)}";
say "fs_square(#{s}): #{fs_square(s...)}";
22 changes: 22 additions & 0 deletions scripts/vigenere_cipher.sf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/ruby

#
## http://rosettacode.org/wiki/Vigen%C3%A8re_cipher
#

func s2v(s) { s.uc.scan(/[A-Z]/)»ord»() »-» 65 };
func v2s(v) { (v »%» 26 »+» 65)»chr»().join };

func blacken (red, key) { v2s(s2v(red) »+« s2v(key)) };
func redden (blk, key) { v2s(s2v(blk) »-« s2v(key)) };

var red = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!";
var key = "Vigenere Cipher!!!";

var black = blacken(red, key);
var ured = redden(black, key);

assert_eq('WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY', black);
assert_eq('BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH', ured);

say "** Test passed!";

0 comments on commit 3b8612a

Please sign in to comment.