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

@ -1,4 +1,4 @@
-- SUBSTRING PRIMITIVES
-- SUBSTRINGS -----------------------------------------------------------------
-- take :: Int -> Text -> Text
on take(n, s)
@ -28,8 +28,7 @@ on init(s)
end init
-- TEST
-- TEST -----------------------------------------------------------------------
on run
set str to "一二三四五六七八九十"
@ -50,9 +49,9 @@ on run
script tabulate
property strPad : " "
on lambda(l, r)
on |λ|(l, r)
l & drop(length of l, strPad) & r
end lambda
end |λ|
end script
linefeed & intercalate(linefeed, ¬
@ -60,25 +59,7 @@ on run
legends, parts)) & linefeed
end run
-- GENERIC LIBRARY FUNCTIONS FOR FORMATTING RESULTS
-- zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
on zipWith(f, xs, ys)
set lng to length of xs
if lng is not length of ys then
missing value
else
tell mReturn(f)
set lst to {}
repeat with i from 1 to lng
set end of lst to lambda(item i of xs, item i of ys)
end repeat
return lst
end tell
end if
end zipWith
-- GENERIC FUNCTIONS FOR TEST -------------------------------------------------
-- intercalate :: Text -> [Text] -> Text
on intercalate(strText, lstText)
@ -88,6 +69,15 @@ on intercalate(strText, lstText)
return strJoined
end intercalate
-- min :: Ord a => a -> a -> a
on min(x, y)
if y < x then
y
else
x
end if
end min
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: Handler -> Script
on mReturn(f)
@ -95,7 +85,19 @@ on mReturn(f)
f
else
script
property lambda : f
property |λ| : f
end script
end if
end mReturn
-- zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
on zipWith(f, xs, ys)
set lng to min(length of xs, length of ys)
set lst to {}
tell mReturn(f)
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, item i of ys)
end repeat
return lst
end tell
end zipWith

View file

@ -1,13 +1,30 @@
10 LET A$="abcdefghijklmnopqrstuvwxyz"
15 LET n=10: LET m=7
20 PRINT A$(n TO n+m-1)
30 PRINT A$(n TO )
40 PRINT A$( TO LEN (A$)-1)
50 FOR i=1 TO LEN (A$)
60 IF A$(i)="g" THEN PRINT A$(i TO i+m-1): LET i=LEN (A$): GO TO 70
70 NEXT i
80 LET B$="ijk"
90 FOR i=1 TO LEN (A$)-LEN (B$)+1
100 IF A$(i TO i+LEN (B$)-1)=B$ THEN PRINT A$(i TO i+m-1): LET i=LEN (A$)-LEN (B$)+1: GO TO 110
110 NEXT i
120 STOP
10 REM SUBSTRING ... ROSETTACODE.ORG
20 A$ = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG"
30 X$ = "J" : S$ = "FOX"
40 N = 5: M = 11
50 PRINT "THE STRING:"
60 PRINT A$
70 PRINT
80 PRINT "SUBSTRING STARTING FROM" N "CHARACTERS IN AND OF" M "LENGTH:"
90 PRINT MID$(A$,N,M)
100 PRINT
110 PRINT "STARTING FROM" N "CHARACTERS IN, UP TO THE END OF THE STRING:"
120 PRINT RIGHT$(A$,LEN(A$)+1-N)
130 PRINT
140 PRINT "WHOLE STRING MINUS LAST CHARACTER:"
150 PRINT LEFT$(A$,LEN(A$)-1)
160 PRINT
170 PRINT "STARTING FROM '";X$;"' AND OF" M "LENGTH:"
180 I = 1
190 IF MID$(A$,I,1)=X$ THEN 220
200 I = I+1
210 GOTO 190
220 PRINT RIGHT$(A$,LEN(A$)+1-I)
230 PRINT
240 PRINT "STARTING FROM '";S$;"' AND OF" M "LENGTH:"
250 I = 1
260 IF MID$(A$,I,LEN(S$))=S$ THEN 290
270 I = I+1
280 GOTO 260
290 PRINT RIGHT$(A$,LEN(A$)+1-I)
300 END

View file

@ -1,12 +1,13 @@
10 LET A$="abcdefghijklmnopqrstuvwxyz": LET la=LEN A$
20 LET n=10: LET m=7
30 PRINT A$(n TO n+m-1)
40 PRINT A$(n TO )
50 PRINT A$( TO la-1)
60 FOR i=1 TO la
70 IF A$(i)="g" THEN PRINT A$(i TO i+m-1): LET i=la
80 NEXT i
90 LET B$="ijk": LET lb=LEN b$
100 FOR i=1 TO la-lb+1
110 IF A$(i TO i+lb-1)=B$ THEN PRINT A$(i TO i+m-1): LET i=la-lb+1
120 NEXT i
10 LET A$="abcdefghijklmnopqrstuvwxyz"
15 LET n=10: LET m=7
20 PRINT A$(n TO n+m-1)
30 PRINT A$(n TO )
40 PRINT A$( TO LEN (A$)-1)
50 FOR i=1 TO LEN (A$)
60 IF A$(i)="g" THEN PRINT A$(i TO i+m-1): LET i=LEN (A$): GO TO 70
70 NEXT i
80 LET B$="ijk"
90 FOR i=1 TO LEN (A$)-LEN (B$)+1
100 IF A$(i TO i+LEN (B$)-1)=B$ THEN PRINT A$(i TO i+m-1): LET i=LEN (A$)-LEN (B$)+1: GO TO 110
110 NEXT i
120 STOP

View file

@ -0,0 +1,12 @@
10 LET A$="abcdefghijklmnopqrstuvwxyz": LET la=LEN A$
20 LET n=10: LET m=7
30 PRINT A$(n TO n+m-1)
40 PRINT A$(n TO )
50 PRINT A$( TO la-1)
60 FOR i=1 TO la
70 IF A$(i)="g" THEN PRINT A$(i TO i+m-1): LET i=la
80 NEXT i
90 LET B$="ijk": LET lb=LEN b$
100 FOR i=1 TO la-lb+1
110 IF A$(i TO i+lb-1)=B$ THEN PRINT A$(i TO i+m-1): LET i=la-lb+1
120 NEXT i

View file

@ -0,0 +1,52 @@
/*
* RosettaCode: Substring, C89
*
* In this task display a substring: starting from n characters in and of m
* length; starting from n characters in, up to the end of the string; whole
* string minus last character; starting from a known character within the
* string and of m length; starting from a known substring within the string
* and of m length.
*
* This example program DOES NOT make substrings. The program simply displays
* certain parts of the input string.
*
*/
#define _CRT_SECURE_NO_WARNINGS /* MSVS compilers need this */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* Put no more than m characters from string to standard output.
*
* It is worth noting that printf("%*s",width,string) does not limit the number
* of characters to be printed.
*
* @param string null terminated string
* @param m number of characters to display
*/
void putm(char* string, size_t m)
{
while(*string && m--)
putchar(*string++);
}
int main(void)
{
char string[] =
"Programs for other encodings (such as 8-bit ASCII, or EUC-JP)."
int n = 3;
int m = 4;
char knownCharacter = '(';
char knownSubstring[] = "encodings";
putm(string+n-1, m ); putchar('\n');
puts(string+n+1); putchar('\n');
putm(string, strlen(string)-1); putchar('\n');
putm(strchr(string, knownCharacter), m ); putchar('\n');
putm(strstr(string, knownSubstring), m ); putchar('\n');
return EXIT_SUCCESS;
}

View file

@ -0,0 +1,56 @@
/*
* RosettaCode: Substring, C89, Unicode
*
* In this task display a substring: starting from n characters in and of m
* length; starting from n characters in, up to the end of the string; whole
* string minus last character; starting from a known character within the
* string and of m length; starting from a known substring within the string
* and of m length.
*
* This example program DOES NOT make substrings. The program simply displays
* certain parts of the input string.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* Put all characters from string to standard output AND write newline.
* BTW, _putws may not be avaliable.
*/
void put(wchar_t* string)
{
while(*string)
putwchar(*string++);
putwchar(L'\n');
}
/*
* Put no more than m characters from string to standard output AND newline.
*/
void putm(wchar_t* string, size_t m)
{
while(*string && m--)
putwchar(*string++);
putwchar(L'\n');
}
int main(void)
{
wchar_t string[] =
L"Programs for other encodings (such as 8-bit ASCII).";
int n = 3;
int m = 4;
wchar_t knownCharacter = L'(';
wchar_t knownSubstring[] = L"encodings";
putm(string+n-1,m);
put (string+n+1);
putm(string, wcslen(string)-1);
putm(wcschr(string, knownCharacter), m );
putm(wcsstr(string, knownSubstring), m );
return EXIT_SUCCESS;
}

View file

@ -0,0 +1,22 @@
<cfoutput>
<cfset str = "abcdefg">
<cfset n = 2>
<cfset m = 3>
<!--- Note: In CF index starts at 1 rather than 0
starting from n characters in and of m length --->
#mid( str, n, m )#
<!--- starting from n characters in, up to the end of the string --->
<cfset countFromRight = Len( str ) - n + 1>
#right( str, countFromRight )#
<!--- whole string minus last character --->
<cfset allButLast = Len( str ) - 1>
#left( str, allButLast )#
<!--- starting from a known character within the string and of m length --->
<cfset startingIndex = find( "b", str )>
#mid( str, startingIndex, m )#
<!--- starting from a known substring within the string and of m length --->
<cfset startingIndexSubString = find( "bc", str )>
#mid( str, startingIndexSubString, m )#
</cfoutput>

View file

@ -0,0 +1,21 @@
<cfscript>
str="abcdefg";
n = 2;
m = 3;
// Note: In CF index starts at 1 rather than 0
// starting from n characters in and of m length
writeOutput( mid( str, n, m ) );
// starting from n characters in, up to the end of the string
countFromRight = Len( str ) - n + 1;
writeOutput( right( str, countFromRight ) );
// whole string minus last character
allButLast = Len( str ) - 1;
writeOutput( left( str, allButLast ) );
// starting from a known character within the string and of m length
startingIndex = find( "b", str );
writeOutput( mid( str, startingIndex, m ) );
// starting from a known substring within the string and of m length
startingIndexSubString = find( "bc", str );
writeOutput( mid( str, startingIndexSubString, m ) );
</cfscript>

View file

@ -1,17 +1,16 @@
#import system.
#import extensions.
import extensions.
#symbol program =
program =
[
#var s := "0123456789".
#var n := 3.
#var m := 2.
#var c := #51.
#var z := "345".
var s := "0123456789".
var n := 3.
var m := 2.
var c := $51.
var z := "345".
console writeLine:(s Substring:m &at:n).
console writeLine:(s Substring:(s length - n) &at:n).
console writeLine:(s Substring:(s length - 1) &at:0).
console writeLine:(s Substring:m &at:(s indexOf:c &at:0)).
console writeLine:(s Substring:m &at:(s indexOf:z &at:0)).
console writeLine(s Substring:m at:n).
console writeLine(s Substring(s length - n) at:n).
console writeLine(s Substring(s length - 1) at:0).
console writeLine(s Substring:m at(s indexOf:c at:0)).
console writeLine(s Substring:m at(s indexOf:z at:0)).
].

View file

@ -0,0 +1,10 @@
Public Sub Main()
Dim sString As String = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG"
Print Mid(sString, 11, 5) 'Starting from n characters in and of m length
Print Mid(sString, 17) 'Starting from n characters in, up to the end of the string
Print Left(sString, -1) 'Whole string minus last character
Print Mid(sString, InStr(sString, "B"), 9) 'Starting from a known character within the string and of m length
Print Mid(sString, InStr(sString, "OVER"), 8) 'Starting from a known substring within the string and of m length
End

View file

@ -0,0 +1,30 @@
import qualified Data.Text as T
(Text, take, drop, init, breakOn, pack, unpack)
fromNforM :: Int -> Int -> T.Text -> T.Text
fromNforM n m s = T.take m (T.drop n s)
fromNtoEnd :: Int -> T.Text -> T.Text
fromNtoEnd = T.drop
allButLast :: T.Text -> T.Text
allButLast = T.init
fromCharForM :: T.Text -> Int -> T.Text -> T.Text
fromCharForM needle m haystack = T.take m $ snd (T.breakOn needle haystack)
fromStringForM :: T.Text -> Int -> T.Text -> T.Text
fromStringForM = fromCharForM
-- TEST -----------------------------------------------------------------------
main :: IO ()
main =
mapM_
(putStrLn . T.unpack)
([ fromNforM 9 10
, fromNtoEnd 20
, allButLast
, fromCharForM (T.pack "") 6
, fromStringForM (T.pack "大势") 6
] <*>
[T.pack "天地不仁仁者人也🐒话说天下大势分久必合🍑合久必分🔥"])

View file

@ -0,0 +1,17 @@
// version 1.0.6
fun main(args: Array<String>) {
val s = "0123456789"
val n = 3
val m = 4
val c = '5'
val z = "12"
var i: Int
println(s.substring(n, n + m))
println(s.substring(n))
println(s.dropLast(1))
i = s.indexOf(c)
println(s.substring(i, i + m))
i = s.indexOf(z)
println(s.substring(i, i + m))
}

View file

@ -0,0 +1,17 @@
> (set 'str "alphabet" 'n 2 'm 4)
4
> ; starting from n characters in and of m length
> (slice str n m)
"phab"
> ; starting from n characters in, up to the end of the string
> (slice str n)
"phabet"
> ; whole string minus last character
> (chop str)
"alphabe"
> ; starting from a known character within the string and of m length
> (slice str (find "l" str) m)
"lpha"
> ; starting from a known substring within the string and of m length
> (slice str (find "ph" str) m)
"phab"

View file

@ -0,0 +1,6 @@
var str = "abcdefgh", n = 2, m = 3;
str[n,m] //-->"cde"
str[n,*] //-->"cdefgh"
str[0,-1] //-->"abcdefg"
str[str.find("d"),m] //-->"def"
str[str.find("de"),m] //-->"def"