時下最熱門的語言是JavaScript,Java和Python,但是編程語言的新陳代謝也在不斷髮展著,新的優秀語言層出不窮,立足取代他們地位。有一首歌唱的好:"由來只有新人笑,有誰知道舊人哭",對編程語言也是如此。那麼在2020的今天,誰是最有前途的語言呢?我們需要拉一個列表,一起說道說道。
Dart
和Ruby藉助RoR一樣,Dart語言也是藉助有一個強有力的框架Flutter以及有個好爹谷歌的緣故,該語言迅速流行起來。
優勢:一種比JavaScript更好的語言。
缺點:與JavaScript及其龐大生態系統直面競爭。
實例:
class Complex {
double _r,_i;
Complex(this._r,this._i);
double get r => _r;
double get i => _i;
String toString() => "($r,$i)";
Complex operator +(Complex other) => new Complex(r+other.r,i+other.i);
Complex operator *(Complex other) =>
new Complex(r*other.r-i*other.i,r*other.i+other.r*i);
double abs() => r*r+i*i;
}
void main() {
double start_x=-1.5;
double start_y=-1.0;
double step_x=0.03;
double step_y=0.1;
for(int y=0;y<20;y++) {
String line="";
for(int x=0;x<70;x++) {
Complex c=new Complex(start_x+step_x*x,start_y+step_y*y);
Complex z=new Complex(0.0, 0.0);
for(int i=0;i<100;i++) {
z=z*(z)+c;
if(z.abs()>2) {
break;
}
}
line+=z.abs()>2 ? " " : "*";
}
print(line);
}
}
Elixir
Elixir有一個美好的名字,翻譯為中文是"靈丹妙藥,長生不老藥",它源自於Erlang,改進了語法以及對有更強大的併發人支持。作為一種純函數式語言,是否可以作為了主流,並長久不衰,長生不老呢?需要我們拭目以待。
優勢:功能編程非常簡單,強大的併發性。
缺點:需要熟悉函數式編程和函數式思維,學習曲線不直。
實例:
defmodule Mandelbrot do
def set do
xsize = 59
ysize = 21
minIm = -1.0
maxIm = 1.0
minRe = -2.0
maxRe = 1.0
stepX = (maxRe - minRe) / xsize
stepY = (maxIm - minIm) / ysize
Enum.each(0..ysize, fn y ->
im = minIm + stepY * y
Enum.map(0..xsize, fn x ->
re = minRe + stepX * x
62 - loop(0, re, im, re, im, re*re+im*im)
end) |> IO.puts
end)
end
defp loop(n, _, _, _, _, _) when n>=30, do: n
defp loop(n, _, _, _, _, v) when v>4.0, do: n-1
defp loop(n, re, im, zr, zi, _) do
a = zr * zr
b = zi * zi
loop(n+1, re, im, a-b+re, 2*zr*zi+im, a+b)
end
end
Mandelbrot.set
Golang
谷歌的又一個嫡兒子。在攻城掠土方面,Golang已經取得了不錯的成績。Golang編譯速度快,便捷的語法,靜態變量,基於協程的高性能併發支持。當然也有槽點,比如繁瑣錯誤語法、混亂模塊機制,和缺乏泛型,當然golang社區也一直在努力改進,這些槽點預計將來都會消失。
優勢:語法簡單,靜態類型,很好的併發性。
缺點:缺少泛型,錯誤語法,模塊機制。
實例:
package main
import (
"fmt"
"image"
"image/color"
"image/draw"
"image/png"
"math/cmplx"
"os"
)
const (
maxEsc = 100
rMin = -2.
rMax = .5
iMin = -1.
iMax = 1.
width = 750
red = 230
green = 235
blue = 255
)
func mandelbrot(a complex128) float64 {
i := 0
for z := a; cmplx.Abs(z) < 2 && i < maxEsc; i++ {
z = z*z + a
}
return float64(maxEsc-i) / maxEsc
}
func main() {
scale := width / (rMax - rMin)
height := int(scale * (iMax - iMin))
bounds := image.Rect(0, 0, width, height)
b := image.NewNRGBA(bounds)
draw.Draw(b, bounds, image.NewUniform(color.Black), image.ZP, draw.Src)
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
fEsc := mandelbrot(complex(
float64(x)/scale+rMin,
float64(y)/scale+iMin))
b.Set(x, y, color.NRGBA{uint8(red * fEsc),
uint8(green * fEsc), uint8(blue * fEsc), 255})
}
}
f, err := os.Create("mandelbrot.png")
if err != nil {
fmt.Println(err)
return
}
if err = png.Encode(f, b); err != nil {
fmt.Println(err)
}
if err = f.Close(); err != nil {
fmt.Println(err)
Julia
Julia是一門強大的數值計算語言。其語法對數學支持非常好,很適合數據科學家編寫應用。是取代Python統計分析和數值計算的預備選手之一。
優勢:對數學友好。
缺點:要與Python競爭。
實例:
using Images
@inline function hsv2rgb(h, s, v)
const c = v * s
const x = c * (1 - abs(((h/60) % 2) - 1))
const m = v - c
const r,g,b =
if h < 60
(c, x, 0)
elseif h < 120
(x, c, 0)
elseif h < 180
(0, c, x)
elseif h < 240
(0, x, c)
elseif h < 300
(x, 0, c)
else
(c, 0, x)
end
(r + m), (b + m), (g + m)
end
function mandelbrot()
const w, h = 1000, 1000
const zoom = 0.5
const moveX = 0
const moveY = 0
const img = Array{RGB{Float64}}(h, w)
const maxIter = 30
for x in 1:w
for y in 1:h
i = maxIter
const c = Complex(
(2*x - w) / (w * zoom) + moveX,
(2*y - h) / (h * zoom) + moveY
)
z = c
while abs(z) < 2 && (i -= 1) > 0
z = z^2 + c
end
const r,g,b = hsv2rgb(i / maxIter * 360, 1, i / maxIter)
img[y,x] = RGB{Float64}(r, g, b)
end
end
save("mandelbrot_set.png", img)
end
mandelbrot()
Kotlin
Kotlin是優化版本的Java,作為Java的取代者之一。谷歌已經在安卓開發中支持Kotlin。
優勢:更強大的Java。
缺點:好要依靠Java的陰影活著。
import java.awt.Graphics
import java.awt.image.BufferedImage
import javax.swing.JFrame
class Mandelbrot: JFrame("Mandelbrot Set") {
companion object {
private const val MAX_ITER = 570
private const val ZOOM = 150.0
}
private val img: BufferedImage
init {
setBounds(100, 100, 800, 600)
isResizable = false
defaultCloseOperation = EXIT_ON_CLOSE
img = BufferedImage(width, height, BufferedImage.TYPE_INT_RGB)
for (y in 0 until height) {
for (x in 0 until width) {
var zx = 0.0
var zy = 0.0
val cX = (x - 400) / ZOOM
val cY = (y - 300) / ZOOM
var iter = MAX_ITER
while (zx * zx + zy * zy < 4.0 && iter > 0) {
val tmp = zx * zx - zy * zy + cX
zy = 2.0 * zx * zy + cY
zx = tmp
iter--
}
img.setRGB(x, y, iter or (iter shl 7))
}
}
}
override fun paint(g: Graphics) {
g.drawImage(img, 0, 0, this)
}
}
fun main(args: Array<string>) {/<string>
Mandelbrot().isVisible = true
}
Lua
Lua是一種小型,簡單,快速,可嵌入,可移植且靈活的語言。
優點:小型、內嵌,nginx編程
缺點:已經26年了,年事已高。
實例:
Pharo
Pharo是Smalltalk的現代化變體,是一種非常有生產力的面嚮對象語言。實際上,Smalltalk是OOP的典範,並且已經啟發了幾乎所有其他OOP語言。但是,也沒有其他一種語言能比Smalltalk更好地實現OOP。
Pharo也是世界上最簡單和最優雅的語言之一,可以讓我們在15分鐘內學習Smalltalk的全部語法,
優勢:生產率高,生產率提高5倍。
缺點:需要不同的編程思想。
實例:
Rust
Rust是一種設計為內存安全的編程語言,通過borrow和變量生命週期控制消除了與存儲器相關的編程錯誤。Rust承諾編程會更安全。而且Rust效率也非常高,語法也非常優雅,目前熱度很高,Github中Rust新項目層出不窮。
優勢:開發更加可靠,有從系統級到應用級,瀏覽器引擎(Firefox),Web開發等各方面的實例。門檻有點高,可以篩選掉一批寫bug的碼農。
缺點:學習曲線比較陡峭,門檻較高,把一批新手擋在外面。
實例:
WebAssembly
WebAssembly可以說是一匹黑馬。預計在接下來十年左右的時間裡,可能會衍生出許多升至頂級的語言。WebAssembly 是一種接近機器語言的跨平臺二進制格式。目前四大主流瀏覽器廠商谷歌Chrome、蘋果Safari、微軟Edge 和 Mozilla FireFox 均以支持了WebAssembly 的初始版本,而且為了安全規範,去年各大廠商又成立了字節碼聯盟,立足於通過協作制定和實施標準,完善 WebAssembly 在瀏覽器之外的生態。
優點:廣泛的瀏覽器和語言支持。
缺點:生態體系尚未完善。
實例:
request = new XMLHttpRequest();
request.open('GET', 'simple.wasm');
request.responseType = 'arraybuffer';
request.send();
request.onload = function() {
var bytes = request.response;
WebAssembly.instantiate(bytes, importObject).then(results => {
results.instance.exports.exported_func();
});
};
閱讀更多 蟲蟲安全 的文章