Coconut – Notazione di funzione – 10

74

Coconut mi piace, tanto che continuando da qui oggi copio qui.

Operatori di funzione
Coconut uses a simple operator function short-hand: surround an operator with parentheses to retrieve its function. Similarly to iterator comprehensions, if the operator function is the only argument to a function, the parentheses of the function call can also serve as the parentheses for the operator function.

c46

A very common thing to do in functional programming is to make use of function versions of built-in operators: currying them, composing them, and piping them. To make this easy, Coconut provides a short-hand syntax to access operator functions.

La lista completa

(|>)     => # pipe forward
(|*>)    => # multi-arg pipe forward
(<|)     => # pipe backward
(<*|)    => # multi-arg pipe backward
(..)     => # function composition
(.)      => (getattr)
(::)     => (itertools.chain) # will not evaluate its arguments lazily
($)      => (functools.partial)
(+)      => (operator.__add__)
(-)      => # 1 arg: operator.__neg__, 2 args: operator.__sub__
(*)      => (operator.__mul__)
(**)     => (operator.__pow__)
(/)      => (operator.__truediv__)
(//)     => (operator.__floordiv__)
(%)      => (operator.__mod__)
(&)      => (operator.__and__)
(^)      => (operator.__xor__)
(|)      => (operator.__or__)
(<<)     => (operator.__lshift__)
(>>)     => (operator.__rshift__)
(<)      => (operator.__lt__)
(>)      => (operator.__gt__)
(==)     => (operator.__eq__)
(<=)     => (operator.__le__)
(>=)     => (operator.__ge__)
(!=)     => (operator.__ne__)
(~)      => (operator.__inv__)
(@)      => (operator.__matmul__)
(not)    => (operator.__not__)
(and)    => # boolean and
(or)     => # boolean or
(is)     => (operator.is_)
(in)     => (operator.__contains__)

c47

 

Creare scorciatoie di funzione
Coconut allows for shorthand in-line function definition, where the body of the function is assigned directly to the function call. The syntax for shorthand function definition is

def <name>(<args>) = <expr>

where <name> is the name of the function, <args> are the functions arguments, and <expr> evaluates the value that the function should return.

Note: Shorthand function definition can be combined with infix and pattern-matching function definition.

Coconut’s shorthand function definition is as easy to write as assignment to a lambda, but will appear named in tracebacks, as it compiles to normal Python function definition.

c48

Finzioni infisse
Coconut allows for infix function calling, where a function is surrounded by backticks and then can have arguments placed in front of or behind it. Backtick calling has a precedence in-between chaining and piping.

Coconut also supports infix function definition to make defining functions that are intended for infix usage simpler. The syntax for infix function definition is

def <arg> `<name>` <arg>:
    <body

where <name> is the name of the function, the <arg>s are the function arguments, and <body> is the body of the function. If an <arg> includes a default, the <arg> must be surrounded in parentheses.

Note: Infix function definition can be combined with shorthand and pattern-matching function definition.

A common idiom in functional programming is to write functions that are intended to behave somewhat like operators, and to call and define them by placing them between their arguments. Coconut’s infix syntax makes this possible.

c49

Con la tastiera italiana ` si ottiene con AltGr+' (nota per me che la memoria…) 😉

Pattern-matching di funzioni
Coconut supports pattern-matching / destructuring assignment syntax inside of function definition. The syntax for pattern-matching function definition is

[match] def <name>(<pattern>, <pattern>, ... [if <cond>]):
    <body>

where <name> is the name of the function, <cond> is an optional additional check, <body> is the body of the function, and <pattern> is defined by Coconut’s match statement. The match keyword at the beginning is optional, but is sometimes necessary to disambiguate pattern-matching function definition from normal function definition, which will always take precedence. Coconut’s pattern-matching function definition is equivalent to a match statement that looks like:

def <name>(*args):
    match (<pattern>, <pattern>, ...) in args:
        <body>
    else:
        err = MatchError(<error message>)
        err.pattern = "def <name>(<pattern>, <pattern>, ...):"
        err.value = args
        raise err

If pattern-matching function definition fails, it will raise a [MatchError]((#matcherror) object just like destructuring assignment.

Note: Pattern-matching function definition can be combined with shorthand and infix function definition.

c50

In Python can’t be done without a long series of checks at the top of the function. See the compiled code for the Python syntax.

:mrgreen:

 

Posta un commento o usa questo indirizzo per il trackback.

Trackback

  • Coconut – Istruzioni – 11 | Ok, panico su 24 agosto 2016 alle 10:22

    […] so se si userà Coconut ma rockz😀 continuo da qui a copiare qui. Istruzioni Coconut supports significantly enhanced destructuring assignment, […]

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo di WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione /  Modifica )

Google photo

Stai commentando usando il tuo account Google. Chiudi sessione /  Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione /  Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione /  Modifica )

Connessione a %s...

Questo sito utilizza Akismet per ridurre lo spam. Scopri come vengono elaborati i dati derivati dai commenti.

%d blogger hanno fatto clic su Mi Piace per questo: