5種你可能從未聽(tīng)說(shuō)過(guò)的編程語(yǔ)言
一起來(lái)看一看你可能最聞所未聞的5種編程語(yǔ)言吧。
1.Nim
我喜歡用Nim編碼,是因為它很有意思。Nim模糊了編譯和腳本語(yǔ)言之間的界線(xiàn)。下面是源代碼:
proc update(options: Options) = ## Downloads the package list from the specified URL. ## ## If the download is successful, the global didUpdatePackages is set to ## true. Otherwise an exception is raised on error. let url = if options.action.typ == actionUpdate and options.action.optionalURL != "": options.action.optionalURL else: defaultPackageURL echo("Downloading package list from " & url) downloadFile(url, options.getNimbleDir() / "packages.json") echo("Done.")
還有長(cháng)一點(diǎn)的:
proc parseConfig*(): Config = result = initConfig() var confFile = getConfigDir() / "nimble" / "nimble.ini" var f = newFileStream(confFile, fmRead) if f == nil: # Try the old deprecated babel.ini confFile = getConfigDir() / "babel" / "babel.ini" f = newFileStream(confFile, fmRead) if f != nil: echo("[Warning] Using deprecated config file at ", confFile) if f != nil: echo("Reading from config file at ", confFile) var p: CfgParser open(p, f, confFile) while true: var e = next(p) case e.kind of cfgEof: break of cfgSectionStart: discard of cfgKeyValuePair, cfgOption: case e.key.normalize of "nimbledir": # Ensure we don't restore the deprecated nimble dir. if e.value != getHomeDir() / ".babel": result.nimbleDir = e.value of "chcp": result.chcp = parseBool(e.value) else: raise newException(NimbleError, "Unable to parse config file:" & " Unknown key: " & e.key) of cfgError: raise newException(NimbleError, "Unable to parse config file: " & e.msg) close(p)
2.Felix
Felix是獨一無(wú)二的。它是C ++、ML以及許多獨特構想的結合。下面摘自我寫(xiě)的一個(gè)小型JSON解析器:
class JSON { typedef LineType = int; union Value = | Object of strdict[Value] | Array of list[Value] | String of string | Number of double | True | False | Null | Error of string * LineType ; union Token = | TString of string | TNumber of double | TLBrace // { | TRBrace // } | TLBrak // [ | TRBrak // ] | TColon // : | TTrue // true | TFalse // false | TNull // null | TEOF | TError of string * LineType ; instance Str[Token] { fun str(t: Token) => match t with | TString ?s => "TString \"" + s + "\"" | TNumber ?n => "TNumber " + n.str | TLBrace => "TLBrace" | TRBrace => "TRBrace" | TLBrak => "TLBrak" | TRBrak => "TRBrak" | TColon => "TColon" | TTrue => "TTrue" | TFalse => "TFalse" | TNull => "TNull" | TEOF => "TEOF" | TError (?s, ?i) => "error at line " + i.str + ": " + s endmatch; } proc lex(s: string, line: &LineType, outs: oschannel[Token]) = { line <- 1; proc tok(t: Token) => write $ outs, t; proc err(s: string) = { tok $ TError(s, *line); return from lex; }; var i = 0.size; while i < s.len do while s.[i].isspace do if s.[i] == "\n" do *line++; done; i++; if i >= s.len goto eof; done; // number if s.[i].isnumeric or (i+1 < s.len and s.[i] == "-" and s.[i+1].isnumeric) do d := s.[i to].double; i += d.str.len; tok $ TNumber d; // string elif s.[i] == "\"" do i++; var st = ""; while i < s.len and s.[i] != "\n" and s.[i] != "\"" do st += s.[i]; i++; done; if s.[i] != "\"" call err "unterminated string literal"; i++; tok $ TString st; // literals elif s.[i to i+4] == "true" do tok $ TTrue; i += 4.size; elif s.[i to i+5] == "false" do tok $ TFalse; i += 5.size; elif s.[i to i+4] == "null" do tok $ TNull; i += 4.size; // others else match s.[i].str with | "{" => tok TLBrace; | "}" => tok TRBrace; | "[" => tok TLBrak; | "]" => tok TRBrak; | ":" => tok TColon; | _ => err "unknown token"; endmatch; i++; done; done; eof:> tok TEOF; } }
你也可以鑒賞一下我寫(xiě)的這個(gè)JSON解析器,這是鏈接。
它有一些很不錯的功能,如schannels(一種協(xié)同程序)。 schannels就像Go的channels,不過(guò)并不是并發(fā)的。Felix還有另一個(gè)類(lèi)似Go的channels,叫做fchannels,這是并發(fā)的。
Felix有一套不錯的工具(Web服務(wù)器,明白易懂的編程格式,α-品質(zhì)的圖形配置工具)和尺寸剛好的標準庫。
至于缺點(diǎn)?文檔很少。不過(guò),郵件討論非?;钴S。
3.Myrddin
Myrddin一些功能:
- 類(lèi)型推斷
- 模式匹配
- Go風(fēng)格slices切片
- C風(fēng)格的內存管理
舉個(gè)libbio輸入/輸出庫的例子。下面是其中的一個(gè)片段:
/* writes to as much from `src` as possible to a file, returning the number of bytes written. */ const write = {f, src std.assert(f.mode & Wr != 0, "File is not in write mode") /* Tack small writes onto the buffer end. Big ones flush the buffer and then go right to kernel. */ if src.len < (f.wbuf.len - f.wend) std.slcp(f.wbuf[f.wend:f.wend+src.len], src) f.wend += src.len -> src.len else flush(f) -> writebuf(f.fd, src) ;; }
4.K
K,連同Kona非常特別。它是將APL推進(jìn)ASCII字符世界的結果。
下面是一些在Kona wiki的慣用語(yǔ):
shuffle:{x@<>(#x)#1 0} / Perfect shuffle mean:{(+/x)%#x} / Arithmetic mean fac:*/1+!: / Factorial fib:{x{x,+/-2#x}/!2} / Fibonacci life:{|/(1;x)&3 4=\:+/,/2{-1 0 1!'\:x}/x} / Conway's Game of Life sort:{x@<x} / Sort list powerset:{x[&:'!2+&#x]} / Powerset
正如你所看到的,K非常簡(jiǎn)潔,就是可能有點(diǎn)太過(guò)了。然而,作為數組處理語(yǔ)言,它功能卓越,而且快速。
5.Objeck
Objeck用起來(lái)特別給力:
class Factorial { function : native : Factorial(n : Int) ~ Int { if (n <= 1) { return n; } else { return n * Factorial(n-1); }; } function : Main(args : String[]) ~ Nil { "Number: "->Print(); number := IO.Console->ReadString()->ToInt(); if (number < 0) { "Number must be greater than 0"->PrintLine(); Runtime->Exit(1); }; Factorial(number)->PrintLine(); } }
它的缺點(diǎn)是沒(méi)有真正的本地編譯器。
其他
還有我沒(méi)有提到過(guò)的ani和Alore。
Ani是一種隱含并行性和非常速度的的編程語(yǔ)言。而Alore是基于靜態(tài)和動(dòng)態(tài)類(lèi)型自由組合的編程語(yǔ)言。
總結
我要介紹的就到這里,希望能能你眼前一亮。