Let's say we have an API endpoint
POST /customers
which creates a customer given a suitable JSON document and returns a 201 CREATED on success.
| if #arg ~= 1 then | |
| io.stderr:write('Exactly one argument required\n') | |
| os.exit(1) | |
| end | |
| function generatePermutations(n, a) | |
| if n == 0 then | |
| print(utf8.char(table.unpack(a))) | |
| else | |
| for i = 1, n do |
| function powerpowermod(a, b, c, p) | |
| if !isprime(p) | |
| error("Go away. I not gonna even try this.") | |
| end | |
| return powermod(a, powermod(b, c, p-1), p) | |
| end |
| Vector = (function (class, meta, prototype) | |
| class.new = function (i, j) | |
| return setmetatable({i = i, j = j}, meta) | |
| end | |
| prototype.magnitude = function (self) | |
| return math.sqrt(self.i * self.i + self.j * self.j) | |
| end | |
| meta.__index = prototype | |
| meta.__add = function (self, v) | |
| return class.new(self.i + v.i, self.j + v.j) |
| arg = {'x': 0} | |
| def f(param): | |
| same_ids = id(arg) == id(param) | |
| different_ids = not same_ids | |
| param = {'y': 4} | |
| arg_changed = 'y' in arg | |
| print('Call by value: {}'.format(different_ids)) | |
| print('Call by sharing: {}'.format(same_ids and not arg_changed)) | |
| print('Call by reference: {}'.format(same_ids and arg_changed)) |
| # Runtimes for various complexity functions, sort of. | |
| # | |
| # A hacked-together little Python3 script printing runtimes for various | |
| # complexity functions to standard output. | |
| # | |
| # The script isn't very general or customizable; you have to edit the | |
| # source code to explore different functions or use different values | |
| # of n. | |
| import math |
| # 10-7-4 Water Jug Problem | |
| # Get 2 units of water in the 7-container or the 4-container | |
| # Starting state is (0,7,4) | |
| # Operations are F10, F7, F4, T107, T104, T710, T74, T410, T47 | |
| # Goal states are (x,2,z) and (x,y,2) | |
| operations = { | |
| 'F10' : lambda x, y, z: [10, y, z], |
| class Pager { | |
| private int offset; | |
| private int limit; | |
| private Pager() {} | |
| public static Pager withOffset(int offset) {return new Pager().offset(offset);} | |
| public Pager offset(int offset) {this.offset = offset; return this;} | |
| public int offset() {return this.offset;} |
| # Makes an enum class with instances (class attributes) having uppercase | |
| # names while the string representations can be in any case. For example, | |
| # Color = enum_class('Color', 'red', 'amber', 'green') returns class Color | |
| # with members Color.RED, Color.AMBER, and Color.GREEN. The __str__ instance | |
| # methods produce 'red', 'green', and 'blue', respectively. To get the | |
| # instance from the string, use, for example, Color.from_string('blue'), | |
| # which will return Color.BLUE. | |
| def enum_class(classname, *values): | |
| cls = type(classname, (), {}) |
| # Makes an enum class whose instances are all constants with uppercase names, | |
| # but whose case of string representations are customizable. For example, | |
| # Color = make_enum_class('red', 'amber', 'green') returns the class Color | |
| # with members Color::RED, Color::AMBER, and Color::GREEN. The to_s methods | |
| # produce 'red', 'green', and 'blue', respectively. To get the instance | |
| # from the string, use, for example, Color.from_string('blue'), which will | |
| # return Color.BLUE. | |
| def make_enum_class(*constants) | |
| cap = ->(s){s.upcase.gsub(/ /, '_')} |