sethverlo
V2EX  ›  问与答

来聊聊编程语言中那些让你觉得被惊呆了的特性

  •  
  •   sethverlo · Aug 29, 2013 · 6406 views
    This topic created in 4823 days ago, the information mentioned may be changed or developed.
    Python:

    1. if 1 < a < 3

    2. return a, b, c

    3. a, b = b, a

    Ruby:

    1. a.try(:b) # 如果 a == nil 就直接返回 nil 而不会告诉你 nil 没有方法…

    2. a.nil? # 这种带问号的方法

    3. a ||= b # 相当于 if !a.nil? then a = b end
    53 replies  •  1970-01-01 08:00:00 +08:00
    timonwong
    timonwong
    Aug 29, 2013❤️1
    1
    D mixins
    aisk
    aisk
    Aug 29, 2013❤️1
    2
    saharabear
    saharabear
    Aug 29, 2013
    3
    这种奇怪的东西,我都看过后不记,也不去用,慢慢就不知道了.
    heganj
    heganj
    Aug 29, 2013❤️1
    4
    66450146
    66450146
    Aug 29, 2013
    5
    ARC
    kunimi
    kunimi
    Aug 29, 2013❤️2
    6
    Python中的倒序:str[::-1]
    Quora上有一个问题叫:What are some cool Python tricks? - https://www.quora.com/Python-programming-language-1/What-are-some-cool-Python-tricks
    shibo501c
    shibo501c
    Aug 29, 2013
    7
    a.try(:b) 中 y(:b)是个很萌的表情啊
    itaotao
    itaotao
    Aug 29, 2013
    8
    @shibo501c 这是啥?
    shibo501c
    shibo501c
    Aug 29, 2013
    9
    @itaotao 仅仅是楼主例子中的,可以忽略
    oobleck
    oobleck
    Aug 29, 2013
    10
    a ||= b 就是 a = a || b
    zencoding
    zencoding
    Aug 29, 2013
    11
    a:b?c
    这句最惊呆,当其大量出现时,程序不可读了
    kran
    kran
    Aug 29, 2013❤️4
    12
    x = x+1;
    我擦, 这是沈麻?怎么可能!
    lichao
    lichao
    Aug 29, 2013
    13
    @kran 哈哈
    timonwong
    timonwong
    Aug 29, 2013
    14
    @kran
    Brilliant!
    kurtis
    kurtis
    Aug 29, 2013❤️1
    15
    我打算开发一种跨时代的新编程语言,取名“点点”,特征之一如下,希望大家投资我开发。

    a = [2,4,6,8 ...];

    b = random in [2,3,5,7,11 ...];

    c = 3; d=4; e=...; f=...;


    if (b in a) { log("b is even");} else {...}
    if (c in a) ...
    ...
    tititake
    tititake
    Aug 29, 2013
    16
    public static boolean checkThis(String... strings)
    sdjl
    sdjl
    Aug 29, 2013
    17
    javascript 闭包
    SErHo
    SErHo
    Aug 29, 2013
    18
    int x = 8
    while (x-->0) {
    hehe(x);
    }
    messense
    messense
    Aug 29, 2013 via iPhone
    19
    .NET 的 Linq
    min
    min
    Aug 29, 2013
    20
    是啊linq那一系列的东西都很难理解的感觉
    msg7086
    msg7086
    Aug 29, 2013
    21
    LINQ碉堡
    cxh116
    cxh116
    Aug 29, 2013❤️1
    22
    ruby
    obj = Object.new
    def obj.hello
    puts "hello world"
    end
    obj.hello

    另外楼主提到的try方法是rails的猴子补丁
    fl3x
    fl3x
    Aug 29, 2013❤️1
    23
    InvokeCode= (fun args -> <@@ (%%(args.[0]) : string) :> obj @@>))

    是要有多囧才能发明<@@ @@>这种东西。。。
    fangzhzh
    fangzhzh
    Aug 29, 2013 via Android
    24
    object-c block
    lisp )))))))))))()((
    moplay
    moplay
    Aug 29, 2013
    25
    """say
    "hello world"
    """"
    Sdhjt
    Sdhjt
    Aug 29, 2013
    26
    C语言:
    (*(void(*)())0)();
    yanng
    yanng
    Aug 29, 2013
    27
    //用完我会关闭
    defer f.Close()

    //我俩并发执行
    go sum(values[:len(values)/2], resultChan)
    go sum(values[len(values)/2:], resultChan)
    beordle
    beordle
    Aug 29, 2013
    28
    @kurtis 囧
    sethverlo
    sethverlo
    OP
    Aug 30, 2013
    29
    @yanng 这是 Go 语言?
    Mutoo
    Mutoo
    Aug 30, 2013
    30
    mov ax 4c00h
    int 21h
    ugoa
    ugoa
    Aug 30, 2013❤️1
    31
    Ruby:

    7.days.ago
    sethverlo
    sethverlo
    OP
    Aug 30, 2013
    32
    @ugoa 赞这个
    hazard
    hazard
    Aug 30, 2013❤️1
    33
    中文编程...
    dndx
    dndx
    Aug 30, 2013
    34
    C:

    int some_shit[10];

    some_shit[20] = 100;
    sethverlo
    sethverlo
    OP
    Aug 30, 2013
    35
    @dndx 被惊呆了……
    chmlai
    chmlai
    Aug 30, 2013
    36
    objc block吧
    fate
    fate
    Aug 30, 2013
    37
    }}}}}}}}}}}}}}}}}}
    yaroot
    yaroot
    Aug 30, 2013
    38
    scala的类型系统, kinds(高阶泛型)
    y combinator的推断(lambda calculus)
    s-expressions

    其他还有很多..erlang里的=, process和其他特性

    另, lz python例子里2和3大体上是一样的吧
    sethverlo
    sethverlo
    OP
    Aug 30, 2013
    39
    @yaroot 没用过 python3。。
    mengzhuo
    mengzhuo
    Aug 30, 2013
    40
    竟然没有人说Python的操作符重载……简直是神一样的存在
    celon
    celon
    Aug 30, 2013
    41
    直接用Java代码去操纵一个class的字节码,改变行为。
    angelface
    angelface
    Aug 30, 2013
    42
    @mengzhuo 和ruby的一比,就不神了。
    timonwong
    timonwong
    Aug 30, 2013
    43
    @mengzhuo
    Welcome to the Scala world...

    还有两个C++反例:
    boost.spirit
    boost.xpressive
    yaroot
    yaroot
    Aug 30, 2013
    44
    @sethverlo

    2. return a, b, c

    3. a, b = b, a

    我说这2个特性是一样的..
    sethverlo
    sethverlo
    OP
    Aug 30, 2013
    45
    @yaroot 当然不一样啊…

    def foo(): return a, b, c

    然后就可以 a, b, c = foo()
    yaroot
    yaroot
    Aug 30, 2013
    46
    @sethverlo 都是tuple
    sethverlo
    sethverlo
    OP
    Aug 30, 2013
    47
    @yaroot 这么说的话确实是……
    mengzhuo
    mengzhuo
    Aug 30, 2013
    48
    @angelface

    Ruby也有+-<> in with 重载?连iter对象都可以重载?

    大Python还有groupby,OTZ不得不再拜一下。
    mengzhuo
    mengzhuo
    Aug 30, 2013
    49
    @angelface 看了一下,也可以,只是程序可读性下降得厉害啊

    def *

    这个……
    plprapper
    plprapper
    Aug 30, 2013
    50
    @zencoding a:b?c 这个。。。 是a?b:c 吧
    tioover
    tioover
    Aug 30, 2013
    51
    Y组合子和meta programming
    angelface
    angelface
    Aug 30, 2013
    52
    @mengzhuo 还好吧, 还可以def >> def ]] def [] def + def .....
    mouer
    mouer
    Aug 30, 2013
    53
    haskell 斐波那契数列 fibs = 1 : 1 : zipWith (+) fibs (tail fibs)
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2459 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 91ms · UTC 12:24 · PVG 20:24 · LAX 04:24 · JFK 07:24
    ♥ Do have faith in what you're doing.