Julia – 91 – Eseguire codice C e Fortran – 1

Continuo da qui, copio qui.

Though most code can be written in Julia, there are many high-quality, mature libraries for numerical computing already written in C and Fortran. To allow easy use of this existing code, Julia makes it simple and efficient to call C and Fortran functions. Julia has a “no boilerplate” philosophy: functions can be called directly from Julia without any “glue” code, code generation, or compilation – even from the interactive prompt. This is accomplished just by making an appropriate call with ccall syntax, which looks like an ordinary function call.

The code to be called must be available as a shared library. Most C and Fortran libraries ship compiled as shared libraries already, but if you are compiling the code yourself using GCC (or Clang), you will need to use the -shared and -fPIC options. The machine instructions generated by Julia’s JIT are the same as a native C call would be, so the resulting overhead is the same as calling a library function from C code. (Non-library function calls in both C and Julia can be inlined and thus may have even less overhead than calls to shared library functions. When both libraries and executables are generated by LLVM, it is possible to perform whole-program optimizations that can even optimize across this boundary, but Julia does not yet support that. In the future, however, it may do so, yielding even greater performance gains.)

Shared libraries and functions are referenced by a tuple of the form (:function, "library") or ("function", "library") where function is the C-exported function name. library refers to the shared library name: shared libraries available in the (platform-specific) load path will be resolved by name, and if necessary a direct path may be specified.

A function name may be used alone in place of the tuple (just :function or "function"). In this case the name is resolved within the current process. This form can be used to call C library functions, functions in the Julia runtime, or functions in an application linked to Julia.

By default, Fortran compilers generate mangled names (for example, converting function names to lowercase or uppercase, often appending an underscore), and so to call a Fortran function via ccall you must pass the mangled identifier corresponding to the rule followed by your Fortran compiler. Also, when calling a Fortran function, all inputs must be passed by reference.

Finally, you can use ccall to actually generate a call to the library function. Arguments to ccall are as follows:

  • A (:function, "library") pair, which must be written as a literal constant,
    OR
    a function pointer (for example, from dlsym).
  • Return type (see below for mapping the declared C type to Julia)
    This argument will be evaluated at compile-time, when the containing method is defined.
  • A tuple of input types. The input types must be written as a literal tuple, not a tuple-valued variable or expression.
    This argument will be evaluated at compile-time, when the containing method is defined.
  • The following arguments, if any, are the actual argument values passed to the function.

As a complete but simple example, the following calls the clock function from the standard C library:

clock takes no arguments and returns an Int32. One common gotcha is that a 1-tuple must be written with a trailing comma. For example, to call the getenv function to get a pointer to the value of an environment variable, one makes a call like this:

Note that the argument type tuple must be written as (Cstring,), rather than (Cstring). [La vorgola!] This is because (Cstring) is just the expression Cstring surrounded by parentheses, rather than a 1-tuple containing Cstring:

In practice, especially when providing reusable functionality, one generally wraps ccall uses in Julia functions that set up arguments and then check for errors in whatever manner the C or Fortran function indicates them, propagating to the Julia caller as exceptions. This is especially important since C and Fortran APIs are notoriously inconsistent about how they indicate error conditions. For example, the getenv C library function is wrapped in the following Julia function, which is a simplified version of the actual definition from env.jl:

The C getenv function indicates an error by returning NULL, but other standard C functions indicate errors in various different ways, including by returning -1, 0, 1 and other special values. This wrapper throws an exception clearly indicating the problem if the caller tries to get a non-existent environment variable:

Here is a slightly more complex example that discovers the local machine’s hostname:

This example first allocates an array of bytes, then calls the C library function gethostname to fill the array in with the hostname, takes a pointer to the hostname buffer, and converts the pointer to a Julia string, assuming that it is a NUL-terminated C string. It is common for C libraries to use this pattern of requiring the caller to allocate memory to be passed to the callee and filled in. Allocation of memory from Julia like this is generally accomplished by creating an uninitialized array and passing a pointer to its data to the C function. This is why we don’t use the Cstring type here: as the array is uninitialized, it could contain NUL bytes. Converting to a Cstring as part of the ccall checks for contained NUL bytes and could therefore throw a conversion error.

🤢

Annunci

SICP – cap. 2 – Esempio: sets come liste ordinate – 80

Continuo da qui, copio qui.

One way to speed up our set operations is to change the representation so that the set elements are listed in increasing order. To do this, we need some way to compare two objects so that we can say which is bigger. For example, we could compare symbols lexicographically, or we could agree on some method for assigning a unique number to an object and then compare the elements by comparing the corresponding numbers. To keep our discussion simple, we will consider only the case where the set elements are numbers, so that we can compare elements using > and <. We will represent a set of numbers by listing its elements in increasing order. Whereas our first representation above allowed us to represent the set {1, 3, 6, 10} by listing the elements in any order, our new representation allows only the list (1 3 6 10).

One advantage of ordering shows up in element-of-set?: In checking for the presence of an item, we no longer have to scan the entire set. If we reach a set element that is larger than the item we are looking for, then we know that the item is not in the set:

(define (element-of-set? x set)
  (cond ((null? set) false)
        ((= x (car set)) true)
        ((< x (car set)) false)
        (else (element-of-set? x (cdr set)))))

How many steps does this save? In the worst case, the item we are looking for may be the largest one in the set, so the number of steps is the same as for the unordered representation. On the other hand, if we search for items of many different sizes we can expect that sometimes we will be able to stop searching at a point near the beginning of the list and that other times we will still need to examine most of the list. On the average we should expect to have to examine about half of the items in the set. Thus, the average number of steps required will be about n / 2. This is still Θ(n) growth, but it does save us, on the average, a factor of 2 in number of steps over the previous implementation.

We obtain a more impressive speedup with intersection-set. In the unordered representation this operation required Θ(n2) steps, because we performed a complete scan of set2 for each element of set1. But with the ordered representation, we can use a more clever method. Begin by comparing the initial elements, x1 and x2, of the two sets. If x1 equals x2, then that gives an element of the intersection, and the rest of the intersection is the intersection of the cdrs of the two sets. Suppose, however, that x1 is less than x2. Since x2 is the smallest element in set2, we can immediately conclude that x1 cannot appear anywhere in set2 and hence is not in the intersection. Hence, the intersection is equal to the intersection of set2 with the cdr of set1. Similarly, if x2 is less than x1, then the intersection is given by the intersection of set1 with the cdr of set2. Here is the procedure:

(define (intersection-set set1 set2)
  (if (or (null? set1) (null? set2))
      '()
      (let ((x1 (car set1)) (x2 (car set2)))
        (cond ((= x1 x2)
               (cons x1 (intersection-set 
                         (cdr set1)
                         (cdr set2))))
              ((< x1 x2) (intersection-set 
                          (cdr set1) 
                          set2))
              ((< x2 x1) (intersection-set 
                          set1 
                          (cdr set2)))))))

To estimate the number of steps required by this process, observe that at each step we reduce the intersection problem to computing intersections of smaller sets—removing the first element from set1 or set2 or both. Thus, the number of steps required is at most the sum of the sizes of set1 and set2, rather than the product of the sizes as with the unordered representation. This is Θ(n) growth rather than Θ(n2) —a considerable speedup, even for sets of moderate size.

🤢

Julia – 90 – Eseguire comandi esterni – 2

Continuo da qui, copio qui.

Quotare
Inevitably, one wants to write commands that aren’t quite so simple, and it becomes necessary to use quotes. Here’s a simple example of a Perl one-liner at a shell prompt:

The Perl expression needs to be in single quotes for two reasons: so that spaces don’t break the expression into multiple shell words, and so that uses of Perl variables like $| (yes, that’s the name of a variable in Perl), don’t cause interpolation. In other instances, you may want to use double quotes so that interpolation does occur:

In general, the Julia backtick syntax is carefully designed so that you can just cut-and-paste shell commands as is into backticks and they will work: the escaping, quoting, and interpolation behaviors are the same as the shell’s. The only difference is that the interpolation is integrated and aware of Julia’s notion of what is a single string value, and what is a container for multiple values. Let’s try the above two examples in Julia:

The results are identical, and Julia’s interpolation behavior mimics the shell’s with some improvements due to the fact that Julia supports first-class iterable objects while most shells use strings split on spaces for this, which introduces ambiguities. When trying to port shell commands to Julia, try cut and pasting first. Since Julia shows commands to you before running them, you can easily and safely just examine its interpretation without doing any damage.

Pipes
o, forse, dovrei dire pipelines.

Shell metacharacters, such as |, &, and >, need to be quoted (or escaped) inside of Julia’s backticks:

This expression invokes the echo command with three words as arguments: hello, |, and sort. The result is that a single line is printed: hello | sort. How, then, does one construct a pipeline? Instead of using '|' inside of backticks, one uses pipeline():

This pipes the output of the echo command to the sort command. Of course, this isn’t terribly interesting since there’s only one line to sort, but we can certainly do much more interesting things:

This prints the highest five user IDs on a UNIX system. The cut, sort and tail commands are all spawned as immediate children of the current julia process, with no intervening shell process. Julia itself does the work to setup pipes and connect file descriptors that is normally done by the shell. Since Julia does this itself, it retains better control and can do some things that shells cannot.

Julia can run multiple commands in parallel:

The order of the output here is non-deterministic because the two echo processes are started nearly simultaneously, and race to make the first write to the STDOUT descriptor they share with each other and the julia parent process. Julia lets you pipe the output from both of these processes to another program:

In terms of UNIX plumbing, what’s happening here is that a single UNIX pipe object is created and written to by both echo processes, and the other end of the pipe is read from by the sort command.

IO redirection can be accomplished by passing keyword arguments stdin, stdout, and stderr to the pipeline function:

pipeline(`do_work`, stdout=pipeline(`sort`, "out.txt"), stderr="errs.txt")

Evitare blocchi nelle pipes
When reading and writing to both ends of a pipeline from a single process, it is important to avoid forcing the kernel to buffer all of the data.

For example, when reading all of the output from a command, call readstring(out), not wait(process), since the former will actively consume all of the data written by the process, whereas the latter will attempt to store the data in the kernel’s buffers while waiting for a reader to be connected.

Another common solution is to separate the reader and writer of the pipeline into separate Tasks:

writer = @async writeall(process, "data")
reader = @async do_compute(readstring(process))
wait(process)
fetch(reader)

Un esempio complesso
The combination of a high-level programming language, a first-class command abstraction, and automatic setup of pipes between processes is a powerful one. To give some sense of the complex pipelines that can be created easily, here are some more sophisticated examples, with apologies for the excessive use of Perl one-liners:

Attenzione: il manuale non lo dice ma si deve lanciare Julia con l’opzione -p N: Integer value N launches N additional local worker processes.

Aggionamento: no, errore mio. Avevo spezzato l’espressione tra i backticks, cosa non ammessa; sostituisco l’immagine con quella aggiornata.

jl579-1

This is a classic example of a single producer feeding two concurrent consumers: one perl process generates lines with the numbers 0 through 9 on them, while two parallel processes consume that output, one prefixing lines with the letter "A", the other with the letter "B". Which consumer gets the first line is non-deterministic, but once that race has been won, the lines are consumed alternately by one process and then the other. (Setting $|=1 in Perl causes each print statement to flush the STDOUT handle, which is necessary for this example to work. Otherwise all the output is buffered and printed to the pipe at once, to be read by just one consumer process.)

Here is an even more complex multi-stage producer-consumer example:

This example is similar to the previous one, except there are two stages of consumers, and the stages have different latency so they use a different number of parallel workers, to maintain saturated throughput.

🤢

Visto nel Web – 308

Il Web è troppo grande, dispersivo. Sto seguendo troppe cose e non riesco a controllare tutto quello che vorrei. Ah! i tempi dei BBS: ero ggiovane 😊 OK, ecco cosa ho wisto nel Web 😊

Exctract the significant parts of a web page
#:programming, codice, snippet
::: The Ubuntu Incident

New ‘Illusion Gap’ Attack Bypasses Windows Defender Scans
#:sicurezza, spionaggio, virus
::: Slashdot

Programming Models for Distributed Computation
wip
#:programming, codice, snippet
::: wallyqs

I made @BartoszMilewski’s ‘Category Theory for Programmers’ into a beautiful PDF
#:programming, codice, snippet
::: hmemcpy

sbcl-1.4.0 out now. Get it while it’s warm!
#:lisp(s)
::: ascii19

“8th” – a gentle introduction to a modern Forth
#:linguaggi di programmazione
::: Lambda the Ultimate

Apple just open sourced every iOS and macOS kernel to date. What the hell
#:free open source software
::: dubstrike

Meet The Next Major Operating System: Amazon’s Alexa
#:sistemi operativi
::: Slashdot

How to teach technical concepts with cartoons
Julia b0rk rockz 🌩 (forse mi ripeto)
#:programming, codice, snippet #:scuola, educazione
::: b0rk

Nim community survey 2017 results are here!
#:linguaggi di programmazione
::: nim_lang

Paul McJones preserves programming knowledge
#:storia
::: RainerJoswig

The Secret Command to Reset Ubuntu Desktop to Default Settings
#:tip, suggerimenti
::: lucaciavatta

Developments and Practices in Astronomy Research Software
specialistico, über 😁
#:linguaggi di programmazione
::: SoftwareSaved

A Dwarf Fortress calendar in PureScript + Halogen
#:programming, codice, snippet
::: Davide Aversa

Concurrent Servers: Part 1 – Introduction
#:programming, codice, snippet
::: Eli Bendersky

Very pleased to announce geometry-processing-js! A geometry processing framework for the web
#:programming, codice, snippet
::: keenanisalive

mpmath 1.0 has been released
#:linguaggi di programmazione
::: SymPy

Simon Phipps Returns to President Role at Open Source Initiative
#:free open source software
::: BrideOfLinux

#OpenData from the Large Hadron Collider sparks new discovery
chissà se vero, se sarà confermato, ma PK per la disponibilità dei dati
#:dati, raccolta
::: Open_Data

Persistent Collections for Java – The new Intel’s library for Java objects stored in persistent memory
#:linguaggi di programmazione
::: mariofusco

Ron Garret famous Common Lisper, wrote google’s billing system, on quantum mechanics at Google talk
confesso che devo ancora vederlo ma lo farò quanto trima — promesso
#:artificial intelligence
::: xah_lee

Facebook and Google promote politicized fake news about Las Vegas shooter
#:fake news, bufale
::: fabiochiusi

New stable 4.7.0 final Scala IDE is out! Enjoy Scala 2.12.3, 2.11.11, Lagom support and more
#:linguaggi di programmazione
::: ScalaIDE

Clean obfuscated code
#:programming, codice, snippet
::: John D. Cook

My iPad Pro experiment
#:hardware
::: Daniel Lemire

Google Chrome Will Block Tab-Under Behavior
#:Web, Internet
::: Slashdot

What he doesn’t say is that in the past 100+ years, technology has overall created MORE jobs than destroyed
#:innovazioni, futuro
::: marcelsalathe ::: marcelsalathe

Python 3.6.3, whee!
#:linguaggi di programmazione
::: gvanrossum

Evidence of Internet Censorship during Catalonia’s Independence Referendum
#:censura
::: fabiochiusi ::: EFF

Yes, please: *put this scandal in context*
#:sicurezza, spionaggio, virus
::: fabiochiusi

Extent to which local commuting patterns can be estimated from data drawn from Twitter
#:social media
::: alexvespi

More Than 80 Percent of All Net Neutrality Comments Were Sent By Bots
#:Web, Internet
::: Slashdot

Companies “prefer to keep themselves — & their users — in the dark by not auditing their systems”
#:ditte
::: FrankPasquale

Easy to think of future tech as mere automation of current tasks but it’s rarely the case. We create alternatives
#:artificial intelligence
::: amasad

deeplearn.js: Deep Learning in JavaScript, in your browser, by Google. Anything that can, will be done in JavaScript
#:linguaggi di programmazione
::: marcelsalathe

The fake news/dark ads hysteria is finally knowing its positive side: the end of the Black Box Society
#:Web, Internet
::: fabiochiusi ::: fabiochiusi

Dati personali su web e telefono, il governo dà il via alla sorveglianza di massa
#:sicurezza, spionaggio, virus
::: tedeschini

DeepMind’s new AI ethics unit is the company’s next big move
#:artificial intelligence
::: demartin

Pazzeschi i nuovi auricolari Google Pixel Buds che effettuano traduzione in real time
::: vincos ::: deantak

Spies Hack. But the Best Spies Hack Other Spies
#:sicurezza, spionaggio, virus
::: Slashdot

Today the FSF turns 32 years old!
#:free open source software
::: fsf

Google Wants Its New Pixelbook to Win the Laptop and Tablet Battle
#:hardware
::: Slashdot

Open-source pioneer Munich has begun its move back to Microsoft
#:sistemi operativi
::: dcavedon

MIT paper on defending internet freedom through decentralization
#:Web, Internet
::: johns_FSF

Facebook Fought Rules That Could Have Exposed Fake Russian Ads
#:sicurezza, spionaggio, virus
::: Slashdot

Bias in AI: when you translate this from English ➡️Turkish, a gender neutral language, then that same Turkish phrase back to English
#:artificial intelligence
::: _DianeKim

The Google Clips Camera Puts AI Behind the Lens
#:artificial intelligence
::: Slashdot ::: pomeranian99

Purism’s Linux Phone Crowdfunder Just Passed $1 Million
#:dispositivi mobili #:free open source software
::: dcavedon

Thinking copyright is about how works are enjoyed rather than how they’re created & distributed is fatal for culture
#:copyright e brevetti
::: webmink

No, the Linux desktop hasn’t jumped in popularity
#:sistemi operativi
::: dcavedon

La valle del Nilo

20 Years of Stuff That Matters
#:Web, Internet
::: slashdot

7 things you might not know about @numba_jit
#:programming, codice, snippet
::: gpuoai

OCaml support on klipse
#:programmazione funzionale
::: reasonml

Soldi pubblici, software libero. O il mio Paese è differente?
#:free open source software
::: suxsonica

Another huge problem for Silicon Valley: we don’t want the future its companies want for us
#:artificial intelligence #:economia
::: fabiochiusi

WiFi4EU, un unico wifi pubblico per tutta Europa
#:Web, Internet
::: la Stampa

Pare che il telefono del capo dello staff della Casa Bianca fosse sotto controllo. Da dicembre
#:sicurezza, spionaggio, virus
::: francescocosta

Mypy 0.530 is out! Protocols, support for TypedDict, and then some
#:linguaggi di programmazione
::: gvanrossum

Tecnologie ed ausili – pensando al [–futuro–] presente
#:scuola, educazione
::: fulcorno

Kaspersky accusata (senza prove) di rubare dati al’NSA. Che se li è fatti fregare come una dilettante
raccontata in modo divertente
#:sicurezza, spionaggio, virus
::: hronir

Respectable accomplished scientist turns into fear-mongering AI prophet @Pinboard is right AI Risk eats smart people
#:artificial intelligence
::: amasad

Our Addiction To Links is Making Good Journalism Harder To Read
#:Web, Internet
::: Slashdot

Technology Review edited version of my earlier blog post on predicting AI future
#:artificial intelligence
::: rodneyabrooks

What if Platforms Like Facebook Are Too Big to Regulate?
#:ditte
::: fabiochiusi ::: justinhendrix

How Switzerland Became The Silicon Valley Of Robotics
#:innovazioni, futuro
::: Forbes

Trump camp social director says Facebook employees were ‘embedded’ at Trump HQ, asked the employees by Republican
#:social media #:politica
::: kylegriffin1

How Smartphones Hijack Our Minds: sources
#:dispositivi mobili
::: roughtype

Java Coders Are Getting Bad Security Advice From Stack Overflow
#:linguaggi di programmazione #:sicurezza, spionaggio, virus
::: Slashdot

Exponential sums make pretty pictures
#:umorismo
::: John D. Cook

Tim O’Reilly: Don’t Fear AI, Fear Ourselves
#:artificial intelligence
::: Slashdot

Q: Do FB users actually care about the fake news & dark ads “scandals”? Is there any measure of how worried they are about propaganda?
#:fake news, bufale #:sicurezza, spionaggio, virus
::: fabiochiusi

Can Cheap Android Tablets Bridge the Digital Divide?
#:scuola, educazione
::: Slashdot

New #AI #software can now predict recipes… simply from #Instagram photos
#:artificial intelligence
::: MikeQuindazzi

Alphabet’s Balloons Will Bring Cellphone Service To Puerto Rico
#:dispositivi mobili
::: slashdot

We are happy to announce Matplotlib 2.1.0
::: matplotlib

Haskell – 8 – elementi fondamentali – 2

Continuo da qui, copio qui, scrollare fino a “Binders — Associating Names with Values or Functions”.

Binders – associare nomi con valori o funzioni
A binder binds a value to a name. The value can subsequently be referred to by that name. For example, the Prelude definition

pi :: Floating a => a
pi = 3.141592653589793

allows us to just write pi instead of spelling out 3.141592653589793. The type class Floating contains the types Float and Double (i.e., single and double precision floating point numbers).

We may use a previously introduced name in another function definition:

circleArea :: Floating a => a -> a
circleArea radius = pi * radius * radius

Sometimes, we need to introduce a new name, which will only be used within a function. In that case, we should use a local binding. For example,

circleArea' :: Floating a => a -> a
circleArea' diameter = pi * radius * radius
  where
    radius = diameter / 2.0       -- local binding

cioè calcola l’area dato il diametro 😊

The evaluation of this function proceeds as follows:

circleArea' 6.0 ⇒   pi * radius * radius where radius = 6.0 / 2.0

                ⇒   pi * radius * radius where radius = 3.0

                ⇒   pi * 3.0 * 3.0

                ⇒   pi * 9.0

                ⇒   3.141592653589793 * 9.0

                ⇒   28.2743

Pausa perché l’argomento successivo è lungo 😊

🤢

Julia – 89 – Eseguire comandi esterni – 1

Continuo da qui, copio qui.

Julia borrows backtick notation for commands from the shell, Perl, and Ruby. However, in Julia, writing

Con Linux ` si ottiene con AltGr'

differs in several aspects from the behavior in various shells, Perl, or Ruby:

  • Instead of immediately running the command, backticks create a Cmd object to represent the command. You can use this object to connect the command to others via pipes, run it, and read or write to it.
  • When the command is run, Julia does not capture its output unless you specifically arrange for it to. Instead, the output of the command by default goes to STDOUT as it would using libc’s system call.
  • The command is never run with a shell. Instead, Julia parses the command syntax directly, appropriately interpolating variables and splitting on words as the shell would, respecting shell quoting syntax. The command is run as julia’s immediate child process, using fork and exec calls.

Here’s a simple example of running an external program:

The hello is the output of the echo command, sent to STDOUT. The run method itself returns nothing, and throws an ErrorException if the external command fails to run successfully.

If you want to read the output of the external command, readstring() can be used instead:

More generally, you can use open() to read from or write to an external command.

Interpolazione
Suppose you want to do something a bit more complicated and use the name of a file in the variable file as an argument to a command. You can use $ for interpolation much as you would in a string literal (see Strings):

A common pitfall when running external programs via a shell is that if a file name contains characters that are special to the shell, they may cause undesirable behavior. Suppose, for example, rather than /etc/passwd, we wanted to sort the contents of the file /Volumes/External HD/data.csv. Let’s try it:

julia> file = "/Volumes/External HD/data.csv"
"/Volumes/External HD/data.csv"

julia> `sort $file`
`sort '/Volumes/External HD/data.csv'`

How did the file name get quoted? Julia knows that file is meant to be interpolated as a single argument, so it quotes the word for you. Actually, that is not quite accurate: the value of file is never interpreted by a shell, so there’s no need for actual quoting; the quotes are inserted only for presentation to the user. This will even work if you interpolate a value as part of a shell word:

julia> path = "/Volumes/External HD"
"/Volumes/External HD"

julia> name = "data"
"data"

julia> ext = "csv"
"csv"

julia> `sort $path/$name.$ext`
`sort '/Volumes/External HD/data.csv'`

As you can see, the space in the path variable is appropriately escaped. But what if you want to interpolate multiple words? In that case, just use an array (or any other iterable container):

julia> files = ["/etc/passwd","/Volumes/External HD/data.csv"]
2-element Array{String,1}:
 "/etc/passwd"
 "/Volumes/External HD/data.csv"

julia> `grep foo $files`
`grep foo /etc/passwd '/Volumes/External HD/data.csv'`

If you interpolate an array as part of a shell word, Julia emulates the shell’s °°{a,b,c} argument generation:

julia> names = ["foo","bar","baz"]
3-element Array{String,1}:
 "foo"
 "bar"
 "baz"

julia> `grep xylophone $names.txt`
`grep xylophone foo.txt bar.txt baz.txt`

Moreover, if you interpolate multiple arrays into the same word, the shell’s Cartesian product generation behavior is emulated:

julia> names = ["foo","bar","baz"]
3-element Array{String,1}:
 "foo"
 "bar"
 "baz"

julia> exts = ["aux","log"]
2-element Array{String,1}:
 "aux"
 "log"

julia> `rm -f $names.$exts`
`rm -f foo.aux foo.log bar.aux bar.log baz.aux baz.log`

Since you can interpolate literal arrays, you can use this generative functionality without needing to create temporary array objects first:

julia> `rm -rf $["foo","bar","baz","qux"].$["aux","log","pdf"]`
`rm -rf foo.aux foo.log foo.pdf bar.aux bar.log bar.pdf baz.aux baz.log baz.pdf qux.aux qux.log qux.pdf`

ahemmmm… non mi riguarda 😐

🤢

Leggere dati in formato CSV con impostazioni locali

Argomento già trattato anche da me in passato ma ogni tanto torna d’attualità.
Necessita di qualche istruzione collegata alle impostazioni locali e altre legate a preferenze personali. Per esempio non usare SciPy, anche se si usa Python, perché poi finisce su Windows. In compenso vale (dovrebbe, spero) sia per M$ Excel che per i fogli di calcolo FOSS; io uno Libre Office.

Ecco un file di dati in formato CSV (dati.csv), salvato con queste impostazioni

1;2;3;4;5,6;"sette";"più parole"
"04/10/17";"questa è la data";"e con separatore migliaia";1.234.567,89;;;
"fine";;;;;;

Nella shell IPython (ma sono OK tutte le altre) ecco, apro il file [1], leggo la prima riga [2] ed elaboro:

Si rende necessario togliere l’a-capo [4]; notare che le stringhe in Python sono immutabili quindi salvo l’elaborazione in sts, st resta invariata; questo anche successivamente passando da sts a stn.

Devo poi importare il modulo re [6], quello delle espressioni regolari che uso [7] per sostiture il separatore decimale virgola con il punto.

Dalla stringa ottengo gli elementi componenti, numeri interi e float e testo con split [9]

I componenti della lista **lst sono stringhe [13, 17] e devono essere convertiti con int [14] per gli interi e float [18] per o floating-point.

Passo ora alla seconda riga.

La data è semplicemente una stringa, sta al programmatore riconoscerla come tale.

Il numero in formato “valuta” non ci consente più di usare la regexp di prima per via della presenza del punto (separatore di migliaia) che si confonderebbe con il separatore decimale che vogliano ottenere. Dobbiamo pertanto eliminarlo [24] (non siamo contabili!) prima di regolarizzare il separatore decimale [25].

Attenzione: il punto per regexp significa “ogni carattere” per cui si è reso necessario “escaparlo [30] così: “\.“.

le liste derivanti da un file CSV hanno tutte la stessa lunghezza, con elementi vuoti corrispondenti alle celle non definite [35].

Facile, vero? 😊

🤢

Haskell – 7 – elementi fondamentali – 1

Continuo da qui, copio qui.

The functions we defined so far were restricted to elementary operations, such as incrementing a number. In this chapter, we will discuss some slightly more advanced functions and survey elementary operations on lists.

I programmi sono composti da moduli
Usually, a program consists of a large number of function and type definitions. Obviously, putting them all into one single file is a bad idea. Modern programming languages therefore provide some means to structure the program by allowing to group related definitions into logical units which are stored in separate files. In Haskell, these units are called modules.

Let us have a look at the definition of a Haskell module, called Simple:

-- Example module
-- Gabriele Keller, August 2015
--
-- This is a simple example of a module definition

module Simple
where

-- calculates the arithmetic mean of two numbers
--
arithmetic_mean :: Fractional a => a -> a -> a
arithmetic_mean x y  = (x + y)  / 2

-- calculates the harmonic mean of two numbers
--
harmonic_mean :: Fractional a => a -> a -> a
harmonic_mean x y  = 2 * x * y / (x + y)

The module begins with a header: a comment containing a one line description of the module, the author and date of creation, and briefly describes the purpose of the module. (Note: The header is optional in that the compiler will not raise an error if it is missing.)

The first line of code starts with the keyword module, followed by the name of the module, the keyword where, and the definitions that belong to the module. Note that a module name, in contrast to function or variable names must start with an uppercase letter.

In Haskell, there is a special module called Prelude whose contents is always available. The module Prelude contains all the functions that are pre-defined in Haskell, such as +, length, and so on.

For now, as we are starting with simple, short programs, we will add all function definitions of a program to a single module. Later we will learn how we can structure more complex programs using multiple modules. In fact, this is a central topic in software development, and modules play an essential role in structuring large software systems.

Indirizzarsi nel flusso di controllo
ho difficoltà a tradurre “branching” 😊

So far, each of our functions has unconditionally performed one specific computation. In general, this is too limiting; so, next, we will look at the role of conditionals in Haskell programs.

choice by way of conditionals
How can we implement a function max which returns the greater of its two arguments? That is, the expressions max 5 2 and max 2 5 both evaluate to 5, max 1 7 to 7, and so on. For two arbitrary numbers x and y, we want max x y to return x if x >= y; otherwise, it should return y. (Note: The function max, as well as signum discussed later, are already defined in the Prelude; so, if we want to define these functions ourselves, we need to add the line import Prelude hiding (max, signum) right after the module header to hide the standard definitions.)

We can generally compare values with types that belong to the type class Ord, which we discussed in previusly; hence, the type of max is

max :: Ord a => a -> a -> a

This can be expressed in Haskell by a so-called conditional or if-then-else expression of the form

if ⟨condition⟩ then ⟨value if true⟩ else ⟨value if false⟩

Now, we can implement max:

max :: Ord a => a -> a -> a
max x y = if x >= y then x else y

Let’s look at the evaluation of max 5 2:

max 5 2 ⇒ if 5 >= 2 then 5 else 2  ⇒ if True then 5 else 2 ⇒ 5

Conditionals are an essential component of programming, because they allow us to dynamically choose between different computations depending on the values of the inputs. Here is another example:

signum :: (Ord a, Num a) => a -> Int
signum x = if x < 0 then -1 else if x == 0 then 0 else 1

A note about the type of signum: the type of the argument x has to be both in Ord, as we use the function < and ==, but also in Num, otherwise we would not be able to compare it to 0, which is in type class Num. We will cover type classes in more depth in a later chapter.

guards
Cascading conditional expressions —as in the previous definition of signum— are difficult to read; therefore, some programming languages, including Haskell, provide an alternative syntax:

signum :: (Ord a, Num a) => a -> Int
signum x | x < 0 = -1 
         | x == 0 = 0 
         | x >  0  = 1

The guards are checked in the order they are listed. For example, if we apply signum to the number 7, then the system first checks if the argument is less than zero. As this is not the case, it checks whether it is equal to zero, before finally the last guard succeeds:

Prelude> signum 7
1

on the basis of the following three side computations evaluating the guards

Prelude> 7 < 0 
False 
Prelude> 7 == 0
False
Prelude> 7 > 0
True

Usually, the last guard should catch all the cases not covered before. For this purpose, we can use the special guard otherwise, which always evaluates to True:

signum :: (Ord a, Num a) => a -> Int
signum x | x <  0     = -1
         | x == 0     = 0
         | otherwise  = 1

Finally, we can rephrase the definition of max using guards:

max :: Ord a => a -> a -> a
max x y | x >= y     = x 
        | otherwise  = y

Which to choose is often a matter of personal style; however, idiomatic Haskell tends to favour code using guards.

A questo punto, per chi vuole, c’è il ripasso in video, nella pagina da cui copio.

🤢

Julia – 88 – Interagire con Julia – 2

Continuo da qui, copio qui.

Completamento con Tab
In both the Julian and help modes of the REPL, one can enter the first few characters of a function or type and then press the tab key to get a list all matches:

Nota: ho premuto Tab che qui visualizzo con 💥 (dopo qualche prova: non sono tanto smart, e mi serve un altro caffè 😊).

The Tab key can also be used to substitute LaTeX math symbols with their Unicode equivalents, and get a list of LaTeX matches as well:

invece dello screenshot copio, più significativo (ma serve questa roba qua?):

julia> \pi[TAB]
julia> π
π = 3.1415926535897...

julia> e\_1[TAB] = [1,0]
julia> e₁ = [1,0]
2-element Array{Int64,1}:
 1
 0

julia> e\^1[TAB] = [1 0]
julia> e¹ = [1 0]
1×2 Array{Int64,2}:
 1  0

julia> \sqrt[TAB]2     # √ is equivalent to the sqrt() function
julia> √2
1.4142135623730951

julia> \hbar[TAB](h) = h / 2\pi[TAB]
julia> ħ(h) = h / 2π
ħ (generic function with 1 method)

julia> \h[TAB]
\hat              \hermitconjmatrix  \hkswarow          \hrectangle
\hatapprox        \hexagon           \hookleftarrow     \hrectangleblack
\hbar             \hexagonblack      \hookrightarrow    \hslash
\heartsuit        \hksearow          \house             \hspace

julia> α="\alpha[TAB]"   # LaTeX completion also works in strings
julia> α="α"

A full list of tab-completions can be found in the Unicode Input section of the manual.

Completion of paths works for strings and julia’s shell mode:

 

julia> path="/[TAB]"
.dockerenv  .juliabox/   boot/        etc/         lib/         media/       opt/         root/        sbin/        sys/         usr/
.dockerinit bin/         dev/         home/        lib64/       mnt/         proc/        run/         srv/         tmp/         var/
shell> /[TAB]
.dockerenv  .juliabox/   boot/        etc/         lib/         media/       opt/         root/        sbin/        sys/         usr/
.dockerinit bin/         dev/         home/        lib64/       mnt/ 

Tab completion can help with investigation of the available methods matching the input arguments:

julia> max([TAB] # All methods are displayed, not shown here due to size of the list

julia> max([1, 2], [TAB] # All methods where `Vector{Int}` matches as first argument
max(x, y) in Base at operators.jl:215
max(a, b, c, xs...) in Base at operators.jl:281

julia> max([1, 2], max(1, 2), [TAB] # All methods matching the arguments.
max(x, y) in Base at operators.jl:215
max(a, b, c, xs...) in Base at operators.jl:281

Keywords are also displayed in the suggested methods, see second line after ; where limit and keep are keyword arguments:

julia> split("1 1 1", [TAB]
split(str::AbstractString) in Base at strings/util.jl:278
split{T<:AbstractString}(str::T, splitter; limit, keep) in Base at strings/util.jl:254

The completion of the methods uses type inference and can therefore see if the arguments match even if the arguments are output from functions. The function needs to be type stable for the completion to be able to remove non-matching methods.

Tab completion can also help completing fields:

The completion of fields for output from functions uses type inference, and it can only suggest fields if the function is type stable.

Personalizzare i colori
The colors used by Julia and the REPL can be customized, as well. To change the color of the Julia prompt you can add something like the following to your .juliarc.jl file, which is to be placed inside your home directory:

function customize_colors(repl)
    repl.prompt_color = Base.text_colors[:cyan]
end

atreplinit(customize_colors)

The available color keys can be seen by typing Base.text_colors in the help mode of the REPL. In addition, the integers 0 to 255 can be used as color keys for terminals with 256 color support.

You can also change the colors for the help and shell prompts and input and answer text by setting the appropriate field of repl in the customize_colors function above (respectively, help_color, shell_color, input_color, and answer_color). For the latter two, be sure that the envcolors field is also set to false.

It is also possible to apply boldface formatting by using Base.text_colors[:bold] as a color. For instance, to print answers in boldface font, one can use the following as a .juliarc.jl:

function customize_colors(repl)
    repl.envcolors = false
    repl.answer_color = Base.text_colors[:bold]
end

atreplinit(customize_colors)

You can also customize the color used to render warning and informational messages by setting the appropriate environment variables. For instance, to render error, warning, and informational messages respectively in magenta, yellow, and cyan you can add the following to your .juliarc.jl file:

ENV["JULIA_ERROR_COLOR"] = :magenta
ENV["JULIA_WARN_COLOR"] = :yellow
ENV["JULIA_INFO_COLOR"] = :cyan

Naturalmente —perso qui— sono assolutamente contrario a queste cose; poi cambi macchina e non le ritrovi 😐

🤢

SICP – cap. 2 – Esempio: raffigurare insiemi – 79 -esercizio

Continuo da qui, copio qui.

Exercise 2.60: We specified that a set would be represented as a list with no duplicates. Now suppose we allow duplicates. For instance, the set {1, 2, 3} could be represented as the list (2 3 2 1 3 2 2). Design procedures element-of-set?, adjoin-set, union-set, and intersection-set that operate on this representation. How does the efficiency of each compare with the corresponding procedure for the non-duplicate representation? Are there applications for which you would use this representation in preference to the non-duplicate one?

Uhmmm… sento Bill the Lizard 👽 🌩

The implementation of element-of-set? doesn’t need to change. It returns #t when it finds an element that matches the input element, otherwise it returns #f. The implementation of adjoin-set is simplified by the new definition. Since we no longer need to check to see if the input element already exists in the set, we can just cons the element to the existing set.

(define (adjoin-set x set)
   (cons x set))

Like adjoin-set, union-set is also simplified by allowing duplicate elements.

(define (union-set set1 set2) 
   (append set1 set2))

We have an interesting choice when it comes to intersection-set. The intersection of two sets under the original (non-duplicate) definition doesn’t seem like it requires any change in implementation since duplicates are now allowed, not necessarily required. However, look what happens when you execute intersection-set with duplicate elements in the first set vs. the second set.

Per verificare devo caricare i codici dei post precedenti.

The result is different depending on which set has duplicate elements. This is because the original implementation checks each element of the first set independently to see if they’re present in the second set. When the first set contains duplicates, so will the result. Since the instructions in the exercise are ambiguous (and being the typical lazy programmer that I am), I’m going to say that this implementation does not need to change.

Since element-of-set? and intersection-set haven’t changed, neither will their performance. Since adjoin-set and union-set no longer need to check for duplicate elements, the performance of both of these procedures is improved. The number of steps required by adjoin-set has gone from O(n) to O(1). The number of steps required by union-set has gone from O(n2) to O(n).

The penalty that we pay for these performance improvements is that sets now require more memory to accommodate duplicate elements. This representation would be preferred over the original in cases where we don’t care about that added memory overhead, and where most of our operations are either adjoin-set or union-set.

Buone le soluzioni di sicp-ex 😁 e Drewiki 😁

🤢