move dmath, dsafe and nixctor to snippets/ directory
This commit is contained in:
parent
43b30424b1
commit
625968bef3
13 changed files with 0 additions and 0 deletions
48
snippets/dmath/README.md
Normal file
48
snippets/dmath/README.md
Normal file
|
@ -0,0 +1,48 @@
|
|||
D programs that calculate various mathematical values
|
||||
|
||||
## factors.d - calculate the factors of a number
|
||||
|
||||
To work out the factors of 31:
|
||||
|
||||
ldc2 -run factors.d 31
|
||||
|
||||
## isfactor.d - determine whether a number is a factor of another
|
||||
|
||||
To determine whether 8 is a factor of 32:
|
||||
|
||||
ldc2 -run isfactor.d 8 32
|
||||
|
||||
## hcf.d - calculate the highest common factor of two numbers
|
||||
|
||||
To work out the HCF of 12 and 28:
|
||||
|
||||
ldc2 -run hcf.d 12 28
|
||||
|
||||
To keep it simple this program will write out a list of factors
|
||||
for each number side-by-side.
|
||||
|
||||
## lcm.d - calculate the lowest common multiple of two numbers
|
||||
|
||||
To work out the LCM of 28 and 42:
|
||||
|
||||
ldc2 -run lcm.d 28 42
|
||||
|
||||
To keep it simple this program will write out a list of multiples
|
||||
for each number side-by-side.
|
||||
|
||||
## primes.d - generate a list of prime numbers up to a maximum value
|
||||
|
||||
To generate a list of primes up to 100:
|
||||
|
||||
ldc2 -run primes.d
|
||||
|
||||
If an argument is supplied, the program will calculate primes up to that number:
|
||||
|
||||
ldc2 -run primes.d 10
|
||||
|
||||
(outputs 2, 3, 5, and 7)
|
||||
Two arguments can be supplied to specify a minimum as well:
|
||||
|
||||
ldc2 -run primes.d 1 20
|
||||
|
||||
(outputs primes from 1 to 20)
|
33
snippets/dmath/factors.d
Normal file
33
snippets/dmath/factors.d
Normal file
|
@ -0,0 +1,33 @@
|
|||
import std.conv : to;
|
||||
import std.stdio : stderr, write, writeln;
|
||||
|
||||
int
|
||||
main(string[] args)
|
||||
{
|
||||
int n;
|
||||
|
||||
if (args.length == 1) {
|
||||
stderr.writeln("usage: factors.d number");
|
||||
return 1;
|
||||
}
|
||||
|
||||
n = args[1].to!int();
|
||||
foreach (int factor; factorsOf(n)) {
|
||||
writeln(factor);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int[]
|
||||
factorsOf(int x)
|
||||
{
|
||||
int[] a;
|
||||
|
||||
foreach (int i; 1 .. x + 1) {
|
||||
if (x % i == 0)
|
||||
a ~= i;
|
||||
}
|
||||
|
||||
return a;
|
||||
}
|
36
snippets/dmath/hcf.d
Normal file
36
snippets/dmath/hcf.d
Normal file
|
@ -0,0 +1,36 @@
|
|||
import std.conv : to;
|
||||
import std.stdio : write, writeln;
|
||||
|
||||
void
|
||||
main(string[] args)
|
||||
{
|
||||
int i, x, y;
|
||||
int[] xf, yf;
|
||||
|
||||
x = args[1].to!int();
|
||||
y = args[2].to!int();
|
||||
|
||||
xf = factorsOf(x);
|
||||
yf = factorsOf(y);
|
||||
for (i = 0; xf.length > i || yf.length > i; i++) {
|
||||
if (i < xf.length)
|
||||
write(xf[i]);
|
||||
write(" ");
|
||||
if (i < yf.length)
|
||||
write(yf[i]);
|
||||
writeln();
|
||||
}
|
||||
}
|
||||
|
||||
int[]
|
||||
factorsOf(int x)
|
||||
{
|
||||
int[] a;
|
||||
|
||||
foreach (int i; 1 .. x + 1) {
|
||||
if (x % i == 0)
|
||||
a ~= i;
|
||||
}
|
||||
|
||||
return a;
|
||||
}
|
29
snippets/dmath/isfactor.d
Normal file
29
snippets/dmath/isfactor.d
Normal file
|
@ -0,0 +1,29 @@
|
|||
import std.conv : to;
|
||||
import std.stdio : stderr, writeln;
|
||||
|
||||
int
|
||||
main(string[] args)
|
||||
{
|
||||
int f, i, n;
|
||||
|
||||
if (args.length < 3) {
|
||||
stderr.writeln("usage: isfactor.d fac num");
|
||||
return 1;
|
||||
}
|
||||
|
||||
f = args[1].to!int();
|
||||
n = args[2].to!int();
|
||||
|
||||
i = f;
|
||||
while (i < n) {
|
||||
i += f;
|
||||
}
|
||||
|
||||
if (i == n) {
|
||||
writeln(f, " is a factor of ", n);
|
||||
return 0;
|
||||
}
|
||||
|
||||
writeln(f, " is NOT a factor of ", n);
|
||||
return 1;
|
||||
}
|
15
snippets/dmath/lcm.d
Normal file
15
snippets/dmath/lcm.d
Normal file
|
@ -0,0 +1,15 @@
|
|||
import std.conv : to;
|
||||
import std.stdio : writeln;
|
||||
|
||||
void
|
||||
main(string[] args)
|
||||
{
|
||||
int x, y;
|
||||
|
||||
x = args[1].to!int();
|
||||
y = args[2].to!int();
|
||||
|
||||
foreach (int i; 1 .. 12) {
|
||||
writeln(x * i, " ", y * i);
|
||||
}
|
||||
}
|
37
snippets/dmath/primes.d
Normal file
37
snippets/dmath/primes.d
Normal file
|
@ -0,0 +1,37 @@
|
|||
import std.conv : to;
|
||||
import std.stdio : write, writeln;
|
||||
|
||||
void
|
||||
main(string[] args)
|
||||
{
|
||||
int min, max;
|
||||
|
||||
min = 1;
|
||||
max = 100;
|
||||
|
||||
if (args.length == 2) {
|
||||
max = args[1].to!int();
|
||||
}
|
||||
if (args.length == 3) {
|
||||
min = args[1].to!int();
|
||||
max = args[2].to!int();
|
||||
}
|
||||
|
||||
foreach (int i; min .. max + 1) {
|
||||
if (factorsOf(i).length == 2)
|
||||
writeln(i);
|
||||
}
|
||||
}
|
||||
|
||||
int[]
|
||||
factorsOf(int x)
|
||||
{
|
||||
int[] a;
|
||||
|
||||
foreach (int i; 1 .. x + 1) {
|
||||
if (x % i == 0)
|
||||
a ~= i;
|
||||
}
|
||||
|
||||
return a;
|
||||
}
|
20
snippets/dsafe/pledge.d
Normal file
20
snippets/dsafe/pledge.d
Normal file
|
@ -0,0 +1,20 @@
|
|||
/*
|
||||
* Snippet of code showing how to call OpenBSD's
|
||||
* pledge() syscall in safe D (or not!)
|
||||
*/
|
||||
|
||||
int
|
||||
main(string[] args) @safe
|
||||
{
|
||||
/* if you're not using @safe you can
|
||||
* remove this ugly lambda thing */
|
||||
version (OpenBSD) () @trusted {
|
||||
import core.sys.openbsd.unistd : pledge;
|
||||
import std.string : toStringz;
|
||||
|
||||
immutable(char) *promises;
|
||||
|
||||
promises = toStringz("stdio rpath wpath cpath ...");
|
||||
pledge(promises, null);
|
||||
}();
|
||||
}
|
11
snippets/dsafe/stderr.d
Normal file
11
snippets/dsafe/stderr.d
Normal file
|
@ -0,0 +1,11 @@
|
|||
/*
|
||||
* Opening stderr in safe D
|
||||
*/
|
||||
|
||||
import std.stdio : File;
|
||||
|
||||
File
|
||||
stderr() @safe
|
||||
{
|
||||
return File("/dev/stderr", "w");
|
||||
}
|
30
snippets/nixctor/README.md
Normal file
30
snippets/nixctor/README.md
Normal file
|
@ -0,0 +1,30 @@
|
|||
## ctor.nix - C/C++/D buildsystem written in Nixlang
|
||||
|
||||
Here is a buildsystem I made that incrementally compiles a program and
|
||||
then links it together at the end. The file default.nix shows an
|
||||
example on how to use this code in a real derivation, it's as simple
|
||||
as just adding your source files into the `sourceFiles` array and
|
||||
ctor.nix will generate a build phase and an install phase for you. It
|
||||
just calls `stdenv.mkDerivation` so you'll have to supply the required
|
||||
arguments to that too.
|
||||
|
||||
To test:
|
||||
|
||||
nix-build
|
||||
|
||||
To use a different compiler or flags, you can use the `compiler` and
|
||||
`cflags` arguments:
|
||||
|
||||
```nix
|
||||
# Example for a D program
|
||||
constructProgram {
|
||||
pname = "hello-world";
|
||||
version = "1.0.0";
|
||||
|
||||
src = fetchFromWhatever {};
|
||||
|
||||
compiler = "ldc2";
|
||||
cflags = "-O -wi";
|
||||
sourceFiles = [ "main.d" "lib.d" ];
|
||||
}
|
||||
```
|
54
snippets/nixctor/ctor.nix
Normal file
54
snippets/nixctor/ctor.nix
Normal file
|
@ -0,0 +1,54 @@
|
|||
# ctor.nix: simple buildsystem written in Nix
|
||||
# It can be adapted for any language, just substitute the default arguments
|
||||
args @
|
||||
{ pkgs ? import <nixpkgs> { }
|
||||
, lib ? pkgs.lib
|
||||
, stdenv ? pkgs.stdenv
|
||||
|
||||
# Name of the compiler to use when compiling source code
|
||||
, compiler ? "cc"
|
||||
# Flags passed to the compiler during compilation stages.
|
||||
, cflags ? "-O2 -c"
|
||||
# Flags passed to the compiler at link time
|
||||
, ldflags ? ""
|
||||
# Flag passed to the compiler to specify the output file
|
||||
, outFlag ? "-o"
|
||||
# Source code files to compile
|
||||
, sourceFiles ? [ "main.c" ]
|
||||
# Suffix of source code files
|
||||
, suffix ? ".c"
|
||||
# Name of the binary file to produce
|
||||
, programName ? "main"
|
||||
|
||||
, ...
|
||||
}:
|
||||
|
||||
with builtins;
|
||||
let
|
||||
newline = "
|
||||
";
|
||||
run = command: ''
|
||||
printf '%s\n' "${command}"
|
||||
${command}
|
||||
'';
|
||||
objify = sourceFile: replaceStrings [ suffix ] [ ".o" ] sourceFile;
|
||||
compile = sourceFile:
|
||||
"${compiler} ${cflags} ${outFlag} ${objify sourceFile} ${sourceFile}";
|
||||
link = objs:
|
||||
"${compiler} ${outFlag} ${programName} ${concatStringsSep " " objs} ${ldflags}";
|
||||
in
|
||||
stdenv.mkDerivation ({
|
||||
buildPhase = ''
|
||||
runHook preBuild
|
||||
${concatStringsSep newline (map
|
||||
(source: run (compile source)) sourceFiles)}
|
||||
${run "${link (map objify sourceFiles)}"}
|
||||
runHook postBuild
|
||||
'';
|
||||
|
||||
installPhase = ''
|
||||
runHook preInstall
|
||||
install -Dm755 ${programName} $out/bin/${programName}
|
||||
runHook postInstall
|
||||
'';
|
||||
} // args)
|
9
snippets/nixctor/default.nix
Normal file
9
snippets/nixctor/default.nix
Normal file
|
@ -0,0 +1,9 @@
|
|||
{ constructProgram ? import ./ctor.nix }:
|
||||
constructProgram {
|
||||
pname = "hello-world";
|
||||
version = "1.0.0";
|
||||
|
||||
src = ./.; # use the current directory
|
||||
|
||||
sourceFiles = [ "main.c" "lib.c" ];
|
||||
}
|
7
snippets/nixctor/lib.c
Normal file
7
snippets/nixctor/lib.c
Normal file
|
@ -0,0 +1,7 @@
|
|||
#include <stdio.h>
|
||||
|
||||
void
|
||||
libfunc(void)
|
||||
{
|
||||
puts("hello, world!");
|
||||
}
|
7
snippets/nixctor/main.c
Normal file
7
snippets/nixctor/main.c
Normal file
|
@ -0,0 +1,7 @@
|
|||
void libfunc(void);
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
libfunc();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue