Rosetta 2
Posted on September 2, 2022
Tags: codeetc
1 build datatype
type Bitcoin int
:= Bitcoin(10) x
data Bitcoin = Bitcoin Int
typedef unsigned int Bitcoin;
2 Insertion sort
= Module[
insertionSort[a_List] := a},
{A = 2, i <= Length[A], i++,
For[i = A[[i]];
value = i - 1;
j >= 1 && A[[j]] > value,
While[j + 1]] = A[[j]];
A[[j --;];
j+ 1]] = value;];
A[[j
A
]@{ 2, 1, 3, 5} insertionSort
3 Http server
package main
import(
"net/http"
"fmt"
"strings"
)
type MyServer struct {
}
func (p *MyServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
:= strings.TrimPrefix(r.URL.Path, "/players/")
player := fmt.Sprintf("<h1> %s </h1>", player)
s .Fprint(w, s)
fmt}
func main(){
:= &MyServer{}
server .ListenAndServe(":5000", server)
http}
override Muxer
package main
import(
"net/http"
"fmt"
"strings"
)
type MyServer struct {
}
func (p MyServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
:= http.NewServeMux()
router .Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
router:= "AHHH"
content := fmt.Sprintf("<h1> %s </h1>", content)
s .WriteHeader(http.StatusOK)
w.Fprint(w, s)
fmt}))
.Handle("/players/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
router:= strings.TrimPrefix(r.URL.Path, "/players/")
player := fmt.Sprintf("<h1> %s </h1>", player)
s .WriteHeader(http.StatusOK)
w.Fprint(w, s)
fmt}))
.ServeHTTP(w, r)
router}
func main(){
:= MyServer{}
server .ListenAndServe(":5000", server)
http}
3.1 Dapr
package main
import(
"net/http"
// "fmt"
// "strings"
"encoding/json"
)
type Subscription struct {
string `json:"pubsubname"`
PubSubName string `json:"topic"`
Topic string `json:"route"`
Route }
type MyServer struct {
}
func (p MyServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
:= []Subscription{
subscriptions {"a","b","c"},
{"d","e","f"},
}
:= http.NewServeMux()
router .Handle("/dapr/subscribe/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
router
.NewEncoder(w).Encode(subscriptions)
json}))
.ServeHTTP(w, r)
router}
func main(){
:= MyServer{}
server .ListenAndServe(":5000", server)
http}
package main
import(
"net/http"
"log"
// "fmt"
// "strings"
"encoding/json"
)
type Subscription struct {
string `json:"pubsubname"`
PubSubName string `json:"topic"`
Topic string `json:"route"`
Route }
type MyServer struct {
}
func (p MyServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
:= []Subscription{
subscriptions {"a","b","c"},
{"d","e","f"},
}
:= http.NewServeMux()
router .Handle("/dapr/subscribe/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
routerswitch r.Method {
case http.MethodGet:
.NewEncoder(w).Encode(subscriptions)
json}
}))
for _,i := range subscriptions{
.Handle(i.PubSubName, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
routerswitch r.Method {
case http.MethodPost:
.Println(r.Body)
log}
}))
}
.ServeHTTP(w, r)
router}
func main(){
:= MyServer{}
server .ListenAndServe(":5000", server)
http}