5種你可能從未聽說過的程式語言

2015-03-11    分類:其他、程式設計開發、首頁精華2人評論發表於2015-03-11

本文由碼農網 – 小峰原創翻譯,轉載請看清文末的轉載要求,歡迎參與我們的付費投稿計劃

一起來看一看你可能最聞所未聞的5種程式語言吧。

1.Nim

我喜歡用Nim編碼,是因為它很有意思。Nim模糊了編譯和指令碼語言之間的界線。下面是原始碼:

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.")

還有長一點的:

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是獨一無二的。它是C ++、ML以及許多獨特構想的結合。下面摘自我寫的一個小型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;
    }
}

你也可以鑑賞一下我寫的這個JSON解析器,這是連結

它有一些很不錯的功能,如schannels(一種協同程式)。 schannels就像Go的channels,不過並不是併發的。Felix還有另一個類似Go的channels,叫做fchannels,這是併發的。

Felix有一套不錯的工具(Web伺服器,明白易懂的程式設計格式,α-品質的圖形配置工具)和尺寸剛好的標準庫。

至於缺點?文件很少。不過,郵件討論非常活躍。

3.Myrddin

Myrddin一些功能:

  • 型別推斷
  • 模式匹配
  • Go風格slices切片
  • C風格的記憶體管理

舉個libbio輸入/輸出庫的例子。下面是其中的一個片段:

/*
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推進ASCII字元世界的結果。

下面是一些在Kona wiki的慣用語:

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非常簡潔,就是可能有點太過了。然而,作為陣列處理語言,它功能卓越,而且快速。

5.Objeck

Objeck用起來特別給力:

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();
    }
}

它的缺點是沒有真正的本地編譯器。

其他

還有我沒有提到過的ani和Alore。

Ani是一種隱含並行性和非常速度的的程式語言。而Alore是基於靜態和動態型別自由組合的程式語言。

總結

我要介紹的就到這裡,希望能能你眼前一亮。

譯文連結:http://www.codeceo.com/article/5-programming-language-never-heard.html
英文原文:The top 5 programming languages you've never heard of
翻譯作者:碼農網 – 小峰
轉載必須在正文中標註並保留原文連結、譯文連結和譯者等資訊。]

相關文章