Example #1
0
/**
 * @author Sérgio Rafael Siqueira <*****@*****.**>
 *
 * @param callable $fn
 *
 * @return mixed
 */
function hold(callable $fn)
{
    $placeholder = pipe(filter(function ($x) {
        return _ === $x;
    }))->pipe('array_keys');
    $args = array_slice(func_get_args(), 1);
    $ks = $placeholder($args);
    return function ($x) use($fn, $args, $ks) {
        if ([] === $ks) {
            return call_user_func_array($fn, array_merge($args, [$x]));
        }
        return call_user_func_array($fn, array_replace($args, [$ks[0] => $x]));
    };
}
Example #2
0
function websocket(Websocket $ws, Websocket\Handshake $handshake)
{
    if ($handshake->hasCrypto()) {
        $promise = Socket\cryptoConnect($handshake->getTarget(), $handshake->getOptions());
    } else {
        $promise = Socket\connect($handshake->getTarget(), $handshake->getOptions());
    }
    return pipe($promise, function ($socket) use($handshake, $ws) {
        return pipe($handshake->send($socket), function ($headers) use($socket, $ws) {
            if (!$headers) {
                throw new Websocket\ClientException();
            }
            return new Websocket\Rfc6455Endpoint($socket, $ws, $headers);
        });
    });
}
Example #3
0
if (file_exists('/proc/cpuinfo')) {
    $procs = preg_match_all('/^processor\\s/m', file_get_contents('/proc/cpuinfo'), $discard);
}
if ($n < $procs) {
    $procs = 1;
}
$chunk_size = (int) ($n / $procs);
$double_size = strlen(pack('d', 0.0));
$chunk_data_size = $double_size * $chunk_size;
$total_data_size = $double_size * $n;
$pipes = array();
$parent = FALSE;
for ($i = 0; $i < $procs; ++$i) {
    $range_begin = $i * $chunk_size;
    if ($i < $procs - 1) {
        $pipe = pipe();
        $pipes[] = $pipe[0];
        $pipe = $pipe[1];
        $range_end = $range_begin + $chunk_size;
        $pid = pcntl_fork();
        if ($pid === -1) {
            die('could not fork');
        } else {
            if ($pid) {
                continue;
            }
        }
        break;
    } else {
        $range_end = $n;
        $parent = TRUE;
Example #4
0
<?php

require_once __DIR__ . '/lambdalicious/src/Verraes/Lambdalicious/load.php';
$winner = pipe(@realpath, @file_get_contents, partial(@explode, "\n"), al, filter(@strlen, __), random);
echo $winner($argv[1]) . "\n";
Example #5
0
					<img alt="Sheet Music Logo" src="images/logo.png">
				</div>
				<div id="topLinks">
					<span id="topLinkStyle">
						<span class="homeLink">
							<?php 
signInStatus();
?>
						</span>
						<span class="signIn">
							<a href="signin.php" title="Sign In"><?php 
signInLink();
?>
</a>
							<?php 
pipe();
?>
							<a href="register.php" title="Register to make an account"><?php 
registerLink();
?>
</a><a href="functions/logout.php" title="Logout"><?php 
logoutLink();
?>
</a> | 
							<?php 
wishlistLink();
?>
 | 
							<a href="basket.php" title="View items in your basket"><?php 
basketLink();
?>
        fwrite($pipes[0], $data);
        fclose($pipes[0]);
        if ($pipes[1] !== null) {
            $stdout = stream_get_contents($pipes[1]);
            fclose($pipes[1]);
        }
        if ($pipes[2] !== null) {
            $stderr = stream_get_contents($pipes[2]);
            fclose($pipes[2]);
        }
        $return_value = proc_close($process);
        if ($return_value != 0) {
            throw new Exception("Unexpected return value {$return_value}.\r\nSTDOUT\r\n{$stdout}\r\nSTDERR\r\n{$stderr}");
        }
    }
}
try {
    $downloader->fetch($criteria, function (Email $email) {
        //        echo $email->getFrom() ."\r\n";
        pipe($email->getSource());
        return true;
    }, Downloader::FETCH_SOURCE | Downloader::FETCH_OVERVIEW);
} catch (ImapException $e) {
    // this is an imap exception/error
    echo $e->getMessage() . "\r\n";
    exit(1);
} catch (Exception $e) {
    // this is an application exception
    echo $e->getMessage() . "\r\n";
    exit(2);
}
Example #7
0
/**
 * Same as `takeUntil` but takes elements from the end of the array.
 * ```php
 * $items = ['Foo', 'Fun', 'Dev', 'Bar', 'Baz'];
 * takeLastUntil(startsWith('F'), $items) // ['Dev', 'Bar', 'Baz']
 * takeLastUntil(startsWith('B'), $items) // []
 * ```
 *
 * @signature (a -> Boolean) -> [a] -> [a]
 * @param  callable $predicate
 * @param  array $list
 * @return array
 */
function takeLastUntil()
{
    $takeLastUntil = function ($predicate, $list) {
        return takeLastWhile(pipe($predicate, not()), $list);
    };
    return apply(curry($takeLastUntil), func_get_args());
}
Example #8
0
/**
 * Wraps the given callable $worker in a promise aware function that has the same number of arguments as $worker,
 * but those arguments may be promises for the future argument value or just values. The returned function will
 * return a promise for the return value of $worker and will never throw. The $worker function will not be called
 * until each promise given as an argument is fulfilled. If any promise provided as an argument fails, the
 * promise returned by the returned function will be failed for the same reason. The promise succeeds with
 * the return value of $worker or failed if $worker throws.
 *
 * @param callable $worker
 *
 * @return callable
 */
function lift(callable $worker) : callable
{
    /**
     * @param mixed ...$args Promises or values.
     *
     * @return \Interop\Async\Promise
     */
    return function (...$args) use($worker) : Promise {
        foreach ($args as $key => $arg) {
            if (!$arg instanceof Promise) {
                $args[$key] = new Success($arg);
            }
        }
        if (1 === \count($args)) {
            return pipe($args[0], $worker);
        }
        return pipe(all($args), function (array $args) use($worker) {
            return $worker(...$args);
        });
    };
}
Example #9
0
/**
 * Takes a set of functions and returns another that is the composition of those `$fns`.
 * The result from the first function execution is used in the second function, etc.
 *
 * @since 0.1
 *
 * @param callable[] $fns functions to be composed
 *
 * @return mixed
 */
function compose(...$fns)
{
    return pipe(...reverse($fns));
}
Example #10
0
/**
 * Gets the snake-case of the string using `$delimiter` as separator.
 * ```
 * $underscoreCase = snakeCase('_');
 * $underscoreCase('IAm-Happy'); // i_am_happy
 * ```
 *
 * @signature String -> String -> String
 * @param  string $delimiter
 * @param  string $string
 * @return string
 */
function snakeCase()
{
    $snackCase = function ($delimiter, $string) {
        return apply(pipe(regReplace('/([A-Z])/', ' \\1'), regReplace('/([0-9]+)/', ' \\1'), regReplace('/[^a-z0-9]+/i', ' '), 'trim', 'strtolower', replace(' ', $delimiter)), [$string]);
    };
    return apply(curry($snackCase), func_get_args());
}
Example #11
0
 private static function spectralnorm($n)
 {
     //$n = (int) (($argc == 2) ? $argv[1] : 1);
     $procs = 1;
     if (file_exists('/proc/cpuinfo')) {
         $procs = preg_match_all('/^processor\\s/m', file_get_contents('/proc/cpuinfo'), $discard);
     }
     if ($n < $procs) {
         $procs = 1;
     }
     $chunk_size = (int) ($n / $procs);
     $double_size = strlen(pack('d', 0.0));
     $chunk_data_size = $double_size * $chunk_size;
     $total_data_size = $double_size * $n;
     $pipes = array();
     $parent = FALSE;
     for ($i = 0; $i < $procs; ++$i) {
         $range_begin = $i * $chunk_size;
         if ($i < $procs - 1) {
             $pipe = pipe();
             $pipes[] = $pipe[0];
             $pipe = $pipe[1];
             $range_end = $range_begin + $chunk_size;
             $pid = pcntl_fork();
             if ($pid === -1) {
                 die('could not fork');
             } else {
                 if ($pid) {
                     continue;
                 }
             }
             break;
         } else {
             $range_end = $n;
             $parent = TRUE;
         }
     }
     $u = array_fill(0, $n, 1.0);
     $_tpl = array_fill($range_begin, $range_end - $range_begin, 0.0);
     $sync = $procs > 0;
     for ($i = 0; $i < 10; $i++) {
         $v = AtAv($n, $u, $range_begin, $range_end, $sync);
         $u = AtAv($n, $v, $range_begin, $range_end, $sync);
     }
     if (!$parent) {
         exit(0);
     }
     $childs = $procs - 1;
     while ($childs--) {
         pcntl_wait($s);
     }
     $vBv = 0.0;
     $vv = 0.0;
     $i = 0;
     foreach ($v as $val) {
         $vBv += $u[$i] * $val;
         $vv += $val * $val;
         ++$i;
     }
     printf("%0.9f\n", sqrt($vBv / $vv));
 }