September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1 @@
call_n(5, o_text, "ha");

View file

@ -1,17 +1,20 @@
on run
nreps("ha", 50000)
end run
replicate(5000, "ha")
-- Repetition by 'Egyptian multiplication' -
-- progressively doubling a list, appending
-- stages of doubling to an accumulator where needed for
-- binary assembly of a target length.
-- String -> Int -> String
on nreps(s, n)
set o to ""
if n < 1 then return o
-- replicate :: Int -> String -> String
on replicate(n, s)
set out to ""
if n < 1 then return out
set dbl to s
repeat while (n > 1)
if (n mod 2) > 0 then set o to o & s
if (n mod 2) > 0 then set out to out & dbl
set n to (n div 2)
set s to (s & s)
set dbl to (dbl & dbl)
end repeat
return o & s
end nreps
return out & dbl
end replicate

View file

@ -0,0 +1,3 @@
p <
p0~1<}~< d@<
_VT@1~>yg~9PKd@M'd;

View file

@ -1,8 +1,7 @@
#import system.
#import system'routines.
#import extensions.
import system'routines.
import extensions.
#symbol program =
program =
[
#var s := 0 repeat &till:5 &each: n [ "ha" ] summarize:(String new) literal.
var s := 0 till:5 repeat(:n)( "ha" ); summarize(String new); literal.
].

View file

@ -0,0 +1,5 @@
Public Sub Main()
Print String$(5, "ha")
End

View file

@ -0,0 +1,9 @@
repString :: String -> Int -> String
repString s n =
let rep x = xs
where
xs = mappend x xs
in take (n * length s) (rep s)
main :: IO ()
main = print $ repString "ha" 5

View file

@ -0,0 +1,25 @@
import Data.Tuple (swap)
import Data.List (unfoldr)
import Control.Monad (join)
-- BY RHIND PAPYRUS 'EGYPTIAN' OR 'ETHIOPIAN' MULTIPLICATION ------------------
repString :: Int -> String -> String
repString n s =
foldr
(\(d, x) a ->
if d > 0 -- Is this power of 2 needed for the binary recomposition ?
then mappend a x
else a)
mempty $
zip
(unfoldr
(\h ->
if h > 0
then Just $ swap (quotRem h 2) -- Binary decomposition of n
else Nothing)
n)
(iterate (join mappend) s) -- Iterative duplication ( mappend to self )
-- TEST -----------------------------------------------------------------------
main :: IO ()
main = print $ repString 500 "ha"

View file

@ -1,9 +1,10 @@
public static String repeat(String str, int times){
StringBuilder ret = new StringBuilder();
for(int i = 0;i < times;i++) ret.append(str);
return ret.toString();
public static String repeat(String str, int times) {
StringBuilder sb = new StringBuilder(str.length() * times);
for (int i = 0; i < times; i++)
sb.append(str);
return sb.toString();
}
public static void main(String[] args){
System.out.println(repeat("ha", 5));
public static void main(String[] args) {
System.out.println(repeat("ha", 5));
}

View file

@ -1,3 +1,3 @@
public static String repeat(String str, int times){
public static String repeat(String str, int times) {
return new String(new char[times]).replace("\0", str);
}

View file

@ -1,12 +1,19 @@
function nreps(s, n) {
var o = '';
if (n < 1) return o;
while (n > 1) {
if (n & 1) o += s;
n >>= 1;
s += s;
}
return o + s;
}
(() => {
'use strict';
nreps('ha', 50000);
// replicate :: Int -> String -> String
const replicate = (n, s) => {
let v = [s],
o = [];
if (n < 1) return o;
while (n > 1) {
if (n & 1) o = o + v;
n >>= 1;
v = v + v;
}
return o.concat(v);
};
return replicate(5000, "ha")
})();

View file

@ -0,0 +1,27 @@
(() => {
'use strict';
// repeat :: Int -> String -> String
const repeat = (n, s) =>
concat(replicate(n, s));
// GENERIC FUNCTIONS ------------------------------------------------------
// concat :: [[a]] -> [a] | [String] -> String
const concat = xs =>
xs.length > 0 ? (() => {
const unit = typeof xs[0] === 'string' ? '' : [];
return unit.concat.apply(unit, xs);
})() : [];
// replicate :: Int -> a -> [a]
const replicate = (n, x) =>
Array.from({
length: n
}, () => x);
// TEST -------------------------------------------------------------------
return repeat(5, 'ha');
})();

View file

@ -0,0 +1,3 @@
operator fun String.times(n: Int) = this.repeat(n)
fun main(args: Array<String>) = println("ha" * 5)

View file

@ -1,2 +0,0 @@
import strutils
repeatStr(5, "ha")

View file

@ -0,0 +1 @@
str_repeat n::int s::string = string $ take n $ cycle (s:[]);

View file

@ -1 +0,0 @@
(make-string 5 #\*) => "*****"

View file

@ -1 +0,0 @@
"ha" * 5 # ==> "hahahahaha"

View file

@ -1 +0,0 @@
5 * "ha" # TypeError: String can't be coerced into Fixnum

View file

@ -0,0 +1 @@
"ha".repeat(5); // ==> "hahahahaha"

View file

@ -0,0 +1,2 @@
$ echo ha | sed 's/.*/&&&&&/'
hahahahaha

View file

@ -0,0 +1,10 @@
10 LET S$="HA"
20 LET N=5
30 GOSUB 60
40 PRINT T$
50 STOP
60 LET T$=""
70 FOR I=1 TO N
80 LET T$=T$+S$
90 NEXT I
100 RETURN

View file

@ -0,0 +1,4 @@
. scalar a="ha"
. scalar b=a*5
. display b
hahahahaha

View file

@ -0,0 +1 @@
"ha" * 5 # --> "hahahahaha"