Lua combines simple procedural syntax with powerful data description constructs based on associative arrays and extensible semantics. Lua is dynamically typed, runs by interpreting bytecode for a register-based virtual machine, and has automatic memory management with incremental garbage collection, making it ideal for configuration, scripting, and rapid prototyping.
She's fast enough for you, old man.
local result = c.conn.Do("ZREVRANGE", %s, 0, 10, "WITHSCORES");
redis.call('ZREMRANGEBYRANK','%s',0, 9);
return result;
#worker_processes 1;
pid /tmp/nginx.pid;
error_log stderr error;
events {
worker_connections 16384;
}
http {
resolver 127.0.0.1;
access_log off;
lua_package_path 'CWD/openresty/?.lua;;';
init_by_lua 'encode = require("cjson").encode mysql = require("resty.mysql")';
server {
listen 8080;
location /plaintext {
default_type "text/plain";
content_by_lua 'ngx.print("Hello, world!")';
}
location /json {
default_type "application/json";
content_by_lua 'ngx.print(encode({message = "Hello, World!"}))';
}
location / {
content_by_lua 'require("app")(ngx)';
}
}
}
local mysql = mysql
local encode = encode
local random = math.random
local mysqlconn = {
host = "DBHOSTNAME",
port = 3306,
database = "hello_world",
user = "user",
password = "pwd"
}
return function(ngx)
local db = mysql:new()
assert(db:connect(mysqlconn))
ngx.print(encode(db:query('SELECT * FROM World WHERE id = '..random(1,10000))[1]))
db:set_keepalive(0, 256)
end
Observable.toObservable("one", "two", "three"
.take(2)
.subscribe((arg) -> {
System.out.println(arg);
});
type Channel struct {
Key string
Messages []Message
Msg map[string]string
conn redis.Conn
}
type Message struct {
Timestamp string `json:"timestamp"`
Message string `json:"message"`
}
func (c *Channel) Get() {
messages, err := redis.Strings(c.conn.Do("ZREVRANGE", c.Key, 0, 10, "WITHSCORES"))
log.Print(len(messages)/2)
m := make([]Message, 0, len(messages)/2)
for index,element := range messages {
if index % 2 == 1 {
msg := &Message{Timestamp: element, Message: messages[index-1]}
m = append(m, *msg)
}
}
c.Messages = m
if err != nil {
log.Fatal(err)
ERROR.Fatal(err)
}
}
func (c Channel) GetMessages() []Message {
return c.Messages
}
package main
import "github.com/codegangsta/martini"
func main() {
m := martini.Classic()
m.Get("/", func() string {
return "Hello world!"
})
m.Run()
}
-define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 5000, Type, [I]}).
-define(CHILD(I, Type, Params), {I, {I, start_link, Params}, permanent, 5000, Type, [I]}).
start_link() ->
io:format("hammer_sup:start_link~n"),
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
start_child() ->
io:format("hammer_sup:start_child~n").
% supervisor:start_child(?MODULE, ?CHILD(hammer_plan_sup, supervisor)).
init(_Args) ->
io:format("hammer_sup:init~n"),
{ ok, {
{plan, Plan},
{session, Requests}
}
} = hammer_config:load(),
Children = [
?CHILD(hammer_planner, worker, [Plan])
],
RestartStrategy = {one_for_one, 0, 1},
{ok, {RestartStrategy, Children}}.
> [a, a] = [1, 2] ** (MatchError) no match of right hand side value: [1, 2]
> [a, b, 3] = [1, 2, 3] > a 1
current_pid = self
spawn fn ->
current_pid <- { :hello, self }
end
receive do
{ :hello, pid } ->
IO.puts "Hello from #{inspect(pid)}"
end