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 @@
#include <iostream>
#include <thread>
#include <vector>
#include <unistd.h>
using namespace std;
void sortThread( int x )
{
usleep( 10000 * x );
cout << x << " ";
}
int main()
{
vector<thread*> threads;
srand( ( unsigned )time( NULL ) );
cout << "unsorted:" << endl;
for( int x = 0; x < 15; x++ )
{
int r = rand() % 20 + 5;
cout << r << " ";
thread* t = new thread( sortThread, r );
threads.push_back( t );
}
cout << endl << endl << "sorted:" << endl;
for( vector<thread*>::iterator t = threads.begin(); t != threads.end(); t++ )
{
( *t )->join();
delete ( *t );
}
cout << endl << endl;
return 0;
}

View file

@ -0,0 +1,9 @@
(ns sleepsort.core
(require [clojure.core.async :as async :refer [chan go <! <!! >! timeout]]))
(defn sleep-sort [l]
(let [c (chan (count l))]
(doseq [i l]
(go (<! (timeout (* 1000 i)))
(>! c i)))
(<!! (async/into [] (async/take (count l) c)))))

View file

@ -0,0 +1,2 @@
(sleep-sort [4 5 3 1 2 7 6])
;=> [1 2 3 4 5 6 7]

View file

@ -0,0 +1,8 @@
(defun sleeprint(n)
(sleep (/ n 10))
(format t "~a~%" n))
(loop for arg in (cdr sb-ext:*posix-argv*) doing
(sb-thread:make-thread (lambda() (sleeprint (parse-integer arg)))))
(loop while (not (null (cdr (sb-thread:list-all-threads)))))

View file

@ -1,9 +1,9 @@
import std.stdio, std.conv, std.datetime, core.thread;
import std.stdio, std.conv, std.datetime, std.array, core.thread;
final class SleepSorter: Thread {
private immutable uint val;
this(in uint n) /*pure nothrow*/ {
this(in uint n) /*pure nothrow @safe*/ {
super(&run);
val = n;
}
@ -15,7 +15,7 @@ final class SleepSorter: Thread {
}
void main(in string[] args) {
if (args.length > 1)
foreach (arg; args[1 .. $])
if (!args.empty)
foreach (const arg; args[1 .. $])
new SleepSorter(arg.to!uint).start;
}

View file

@ -0,0 +1,18 @@
import 'dart:async';
Future<List<int>> sleepsort(List<int> input) {
List<Future<int>> tasks = [];
List<int> result = [];
for (int i in input) {
tasks.add(new Future.delayed(new Duration(seconds: i), () {
result.add(i);
}));
}
return Future.wait(tasks).then((_) {
return result;
});
}
sleepsort.sleepsort([3, 1, 2]).then((List<int> sorted) {
print(sorted);
});

View file

@ -1,27 +1,26 @@
package main
import (
"log"
"os"
"strconv"
"sync"
"time"
"fmt"
"log"
"os"
"strconv"
"time"
)
func main() {
lg := log.New(os.Stdout, "", 0)
var wg sync.WaitGroup
wg.Add(len(os.Args) - 1)
for _, a := range os.Args[1:] {
if i, err := strconv.ParseInt(a, 10, 64); err != nil {
lg.Print(err)
wg.Done()
} else {
time.AfterFunc(time.Duration(i*int64(time.Second)), func() {
lg.Print(i)
wg.Done()
})
}
}
wg.Wait()
out := make(chan uint64)
for _, a := range os.Args[1:] {
i, err := strconv.ParseUint(a, 10, 64)
if err != nil {
log.Fatal(err)
}
go func(n uint64) {
time.Sleep(time.Duration(n) * time.Millisecond)
out <- n
}(i)
}
for _ = range os.Args[1:] {
fmt.Println(<-out)
}
}

View file

@ -0,0 +1,9 @@
@Grab(group = 'org.codehaus.gpars', module = 'gpars', version = '1.2.1')
import groovyx.gpars.GParsPool
GParsPool.withPool args.size(), {
args.eachParallel {
sleep(it.toInteger() * 10)
println it
}
}

View file

@ -0,0 +1,27 @@
function sleeprint(n)
local t0 = os.time()
while os.time() - t0 <= n do
coroutine.yield(false)
end
print(n)
return true
end
coroutines = {}
for i=1, #arg do
wrapped = coroutine.wrap(sleeprint)
table.insert(coroutines, wrapped)
wrapped(tonumber(arg[i]))
end
done = false
while not done do
done = true
for i=#coroutines,1,-1 do
if coroutines[i]() then
table.remove(coroutines, i)
else
done = false
end
end
end

View file

@ -0,0 +1,29 @@
socket = require 'socket'
function sleeprint(n)
local t0 = socket.gettime()
while (socket.gettime() - t0)*100 <= n do
coroutine.yield(false)
end
print(n)
return true
end
coroutines = {}
for i=1, #arg do
wrapped = coroutine.wrap(sleeprint)
table.insert(coroutines, wrapped)
wrapped(tonumber(arg[i]))
end
done = false
while not done do
done = true
for i=#coroutines,1,-1 do
if coroutines[i]() then
table.remove(coroutines, i)
else
done = false
end
end
end

View file

@ -1,7 +1,2 @@
use MuEvent;
for <6 8 1 12 2 14 5 2 1 0> -> $item {
MuEvent::timer( after => $item, cb => sub { say $item } );
}
MuEvent::run;
await map -> $delay { start { sleep $delay ; say $delay } },
<6 8 1 12 2 14 5 2 1 0>;

View file

@ -1,46 +1,46 @@
/*REXX program implements a sleep sort (with numbers entered from C.L.)*/
/*REXX program implements a sleep sort (with numbers entered from C.L.).*/
numeric digits 300 /*over the top, but what the hey!*/
/* (above) ... from vaudeville.*/
/* (above) ··· from vaudeville.*/
#.= /*placeholder for the array of #s*/
stuff='1e9 50 5 40 4 1 100 30 3 12 2 8 9 7 6 6 10 20 0' /*alphabetically*/
stuff= 1e9 50 5 40 4 1 100 30 3 12 2 8 9 7 6 6 10 20 0 /*alphabetically*/
parse arg numbers /*let the user specify on the CL.*/
if numbers='' then numbers=stuff /*Not specified? Then use default*/
if numbers='' then numbers=stuff /*Not specified? Then use default*/
N=words(numbers) /*N is the number of numbers. */
w=length(N) /*width of N (for nice output).*/
say N 'numbers to be sorted:' numbers /*informative informational info.*/
do j=1 for N /*let's start to boogie-woogie. */
do j=1 for N /*let's start to boogie-woogie. */
#.j=word(numbers,j) /*plug in one number at a time. */
if datatype(#.j,'Numeric') then #.j = #.j/1 /*normalize it if num.*/
call fork /*REGINA REXX supports FORK func.*/
if datatype(#.j,'N') then #.j=#.j/1 /*normalize it if a number.*/
call fork /*only REGINA REXX supports FORK.*/
call sortItem j /*start a sort for array number. */
end /*j*/
do forever while \inOrder(N) /*wait for the sorts to complete.*/
call sleep 1 /*1 sec is the min effective time*/
do forever while \inOrder(N) /*wait for the sorts to complete.*/
call sleep 1 /*1 sec is minimum effective time*/
end /*forever while*/ /*well, other than zero seconds. */
m=max(length(#.1),length(#.N)) /*width of smallest | largest num*/
say; say 'after sort:'; indent=left('',20) /*same as: COPIES(' ',20) */
/*∙∙∙but LEFT pads [much faster]*/
do k=1 for N /*list (sorted) array's elements.*/
say indent 'array element' right(k,w) '>' right(#.k,m)
end /*k*/
say; say 'after sort:' /*display blank line and a title.*/
do k=1 for N /*list (sorted) array's elements.*/
say left('',20) 'array element' right(k,w) '' right(#.k,m)
end /*k*/
exit /*stick a fork in it, we're done.*/
/*───────────────────────────────────SortItem subroutine────────────────*/
sortItem: procedure expose #.; parse arg ? /*sorts single item.*/
do Asort=1 until \switched /*cook until cooked.*/
switched=0 /*honky-dorey so far*/
do i=1 while #.i\=='' & \switched
if #.? >= #.i then iterate /*this one ok*/
parse value #.? #.i with #.i #.?
switched=1 /*yup, we done switched one.*/
end /*i*/
if Asort//?==0 then call sleep switched /*sleep if last*/
end /*Asort*/
sortItem: procedure expose #.; parse arg ? /*sorts single item.*/
do Asort=1 until \switched /*cook until cooked.*/
switched=0 /*hunky-dorey so far*/
do i=1 while #.i\=='' & \switched
if #.? >= #.i then iterate /*this one ok*/
parse value #.? #.i with #.i #.?
switched=1 /* [↑] swapped one.*/
end /*i*/
if Asort//?==0 then call sleep switched /*sleep if last*/
end /*Asort*/
return /*Sleeping Beauty awakes. Not to worry: (c) = circa 1697.*/
/*───────────────────────────────────InOrder subroutine─────────────────*/
inOrder: procedure expose #.; parse arg howMany /*is array in order? */
do m=1 for howMany-1; next=m+1; if #.m>#.next then return 0
do m=1 for howMany-1; next=m+1; if #.m>#.next then return 0
end /*m*/ /*keep looking for fountain of yut*/
return 1 /*yes, indicate with an indicator.*/