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

@ -0,0 +1,38 @@
//
// How to compile:
// patscc -DATS_MEMALLOC_LIBC -o string_reverse string_reverse.dats
//
#include
"share/atspre_staload.hats"
fun
string_reverse
(
x: string
) : Strptr1 = let
//
val [n:int] x = g1ofg0(x)
val y = string1_copy(x)
val n = string1_length(x)
val (pf, fpf | p) =
$UNSAFE.ptr_vtake{array(char,n)}(ptrcast(y))
val () = array_subreverse(!p, i2sz(0), n)
prval () = fpf(pf)
//
in
$UNSAFE.castvwtp0{Strptr1}(y)
end (* end of [string_reverse] *)
(* ****** ****** *)
implement
main0 () = let
//
val rev = string_reverse("asdf")
val ((*void*)) = println! ("reverse(\"asdf\") = \"", rev, "\"")
val ((*freed*)) = strptr_free (rev)
//
in
// nothing
end // end of [main0]

View file

@ -0,0 +1,11 @@
# Usage: awk -f reverse.awk -v s=Rosetta
function rev(s, i,a,r) {
split(s, a, "")
for (i in a) r = a[i] r
return r
}
BEGIN {
if(!s) s = "Hello, world!"
print s, "<-->", rev(s)
}

View file

@ -7,7 +7,7 @@ reverse(text s)
i = length(s);
while (i) {
i -= 1;
b_insert(b, -1, character(s, i));
b_insert(b, -1, s[i]);
}
return b_string(b);

View file

@ -0,0 +1 @@
String reverse(String s) => new String.fromCharCodes(s.runes.toList().reversed);

View file

@ -0,0 +1,17 @@
import 'package:unittest/unittest.dart';
String reverse(String s) => new String.fromCharCodes(s.runes.toList().reversed);
main() {
group("Reverse a string -", () {
test("Strings with ASCII characters are reversed correctly.", () {
expect(reverse("hello, world"), equals("dlrow ,olleh"));
});
test("Strings with non-ASCII BMP characters are reversed correctly.", () {
expect(reverse("\u4F60\u4EEC\u597D"), equals("\u597D\u4EEC\u4F60"));
});
test("Strings with non-BMP characters are reversed correctly.", () {
expect(reverse("hello, \u{1F310}"), equals("\u{1F310} ,olleh"));
});
});
}

View file

@ -1,11 +0,0 @@
String reverse(String s) {
StringBuffer sb=new StringBuffer();
for(int i=s.length-1;i>=0;i--) {
sb.add(s[i]);
}
return sb.toString();
}
main() {
print(reverse('a string.'));
}

View file

@ -0,0 +1,2 @@
IO.puts (String.reverse "asdf")
IO.puts (String.reverse "as⃝df̅")

View file

@ -1,3 +1,18 @@
var a = "cat".split("");
a.reverse();
print(a.join("")); // tac
//using chained methods
function reverseStr(s) {
return s.split('').reverse().join('');
}
//fast method using for loop
function reverseStr(s) {
for (var i = s.length - 1, o = ''; i >= 0; o += s[i--]) { }
return o;
}
//fast method using while loop (faster with long strings in some browsers when compared with for loop)
function reverseStr(s) {
var i = s.length, o = '';
while (i--) o += s[i];
return o;
}

View file

@ -0,0 +1,2 @@
julia> join(reverse(collect(graphemes("nöel"))))
"leön"

View file

@ -0,0 +1,3 @@
main: func {
"asdf" reverse() println() // prints "fdsa"
}

View file

@ -0,0 +1,16 @@
procedure revString(var s:string);
var
i,j:integer;
tmp:char;
begin
i := 1;
j := length(s);
while i<j do
begin
tmp:=s[i];
s[i]:=s[j];
s[j]:=tmp;
inc(i);
dec(j)
end;
end;

View file

@ -1,3 +1,4 @@
output :
L = [100,99,98,97],
S = "dcba".
accRev([H|T], A, R) :- accRev(T, [H|A], R).
accRev([], A, A).
rev(L,R) :- accRev(L,[],R).

View file

@ -1 +1 @@
"asdf".foldRight(""){ (a,b)=> b+a }
"asdf".foldRight("")((a,b) => b+a)

View file

@ -1,30 +1,11 @@
def reverseString(s: String) = {
import java.lang.Character._
val combiningTypes = List(NON_SPACING_MARK, ENCLOSING_MARK, COMBINING_SPACING_MARK)
def isCombiningCharacter(c: Char) = combiningTypes contains c.getType
def isCombiningSurrogate(high: Char, low: Char) = combiningTypes contains getType(toCodePoint(high, low))
def isCombining(l: List[Char]) = l match {
case List(a, b) => isCombiningSurrogate(a, b)
case List(a) => isCombiningCharacter(a)
case Nil => true
case _ => throw new IllegalArgumentException("isCombining expects a list of up to two characters")
def reverse(s: String) = {
import java.text.{Normalizer,BreakIterator}
val norm = Normalizer.normalize(s, Normalizer.Form.NFKC) // waffle -> waffle (optional)
val it = BreakIterator.getCharacterInstance
it setText norm
def break(it: BreakIterator, prev: Int, result: List[String] = Nil): List[String] = it.next match {
case BreakIterator.DONE => result
case cur => break(it, cur, norm.substring(prev, cur) :: result)
}
def cleanSurrogate(l: List[Char]) = l match {
case List(a, b) if a.isHighSurrogate && b.isLowSurrogate => l
case List(a, b) if a.isLowSurrogate => Nil
case List(a, b) => List(a)
case _ => throw new IllegalArgumentException("cleanSurrogate expects lists of two characters, exactly")
}
def splitString(string: String) = (string+" ").iterator sliding 2 map (_.toList) map cleanSurrogate toList
def recurse(fwd: List[List[Char]], rev: List[Char]): String = fwd match {
case Nil => rev.mkString
case c :: rest =>
val (combining, remaining) = rest span isCombining
recurse(remaining, c ::: combining.foldLeft(List[Char]())(_ ::: _) ::: rev)
}
recurse(splitString(s), Nil)
break(it, it.first).mkString
}

View file

@ -0,0 +1,30 @@
def reverseString(s: String) = {
import java.lang.Character._
val combiningTypes = List(NON_SPACING_MARK, ENCLOSING_MARK, COMBINING_SPACING_MARK)
def isCombiningCharacter(c: Char) = combiningTypes contains c.getType
def isCombiningSurrogate(high: Char, low: Char) = combiningTypes contains getType(toCodePoint(high, low))
def isCombining(l: List[Char]) = l match {
case List(a, b) => isCombiningSurrogate(a, b)
case List(a) => isCombiningCharacter(a)
case Nil => true
case _ => throw new IllegalArgumentException("isCombining expects a list of up to two characters")
}
def cleanSurrogate(l: List[Char]) = l match {
case List(a, b) if a.isHighSurrogate && b.isLowSurrogate => l
case List(a, b) if a.isLowSurrogate => Nil
case List(a, b) => List(a)
case _ => throw new IllegalArgumentException("cleanSurrogate expects lists of two characters, exactly")
}
def splitString(string: String) = (string+" ").iterator sliding 2 map (_.toList) map cleanSurrogate toList
def recurse(fwd: List[List[Char]], rev: List[Char]): String = fwd match {
case Nil => rev.mkString
case c :: rest =>
val (combining, remaining) = rest span isCombining
recurse(remaining, c ::: combining.foldLeft(List[Char]())(_ ::: _) ::: rev)
}
recurse(splitString(s), Nil)
}