SlideShare une entreprise Scribd logo
1  sur  58
Télécharger pour lire hors ligne
Functional
                    Programming


Friday, 18 May 12
Andrew Jones @andrew_jones
                    Thom Leggett @thomleggett



Friday, 18 May 12
BrisFunctional
                          @brisfunctional
                     brisfunctional.github.com




Friday, 18 May 12
Project Euler



Friday, 18 May 12
JSON parser



Friday, 18 May 12
Noughts and Crosses
                            AI


Friday, 18 May 12
http://www.flickr.com/photos/dippy_duck/7210706592/




Friday, 18 May 12
Alligator Eggs

Friday, 18 May 12
Friday, 18 May 12
http://www.flickr.com/photos/14591708@N00/6878586390/




Friday, 18 May 12
What is Functional
                     Programming?


Friday, 18 May 12
Functional




Friday, 18 May 12
Disfunctional   Functional




Friday, 18 May 12
Disfunctional        Functional


                      C#




Friday, 18 May 12
Disfunctional        Functional


                      C#




Friday, 18 May 12
Disfunctional        Functional


                      C#    Haskell




Friday, 18 May 12
Disfunctional                     Functional


                      C#                 Haskell
                           Javascript




Friday, 18 May 12
Disfunctional                         Functional


                      C#                     Haskell
                               Javascript
                           C




Friday, 18 May 12
Disfunctional                                Functional


                             C#                     Haskell
                    Pascal            Javascript
                                  C




Friday, 18 May 12
Disfunctional          Java                    Functional


                             C#                       Haskell
                    Pascal              Javascript
                                    C




Friday, 18 May 12
Disfunctional          Java                     Functional


                             C#                        Haskell
                    Pascal              Javascript
                                    C
                                                     Erlang



Friday, 18 May 12
Disfunctional          Java                     Functional


                             C#                        Haskell
                    Pascal              Javascript
                                                       Clojure
                                    C
                                                     Erlang



Friday, 18 May 12
Disfunctional          Java          SQL        Functional


                             C#                        Haskell
                    Pascal              Javascript
                                                       Clojure
                                    C
                                                     Erlang



Friday, 18 May 12
Python

      Disfunctional          Java          SQL        Functional


                             C#                        Haskell
                    Pascal              Javascript
                                                       Clojure
                                    C
                                                     Erlang



Friday, 18 May 12
Ruby
                                          Python

      Disfunctional          Java          SQL        Functional


                             C#                        Haskell
                    Pascal              Javascript
                                                       Clojure
                                    C
                                                     Erlang



Friday, 18 May 12
Ruby
                                                Python

      Disfunctional                Java          SQL        Functional


                                   C#                        Haskell
                    Pascal                    Javascript
                                                             Clojure
                                          C
                             PHP                           Erlang



Friday, 18 May 12
http://alblue.bandlem.com/2012/03/qcon-day-3.html




Friday, 18 May 12
Immutability



Friday, 18 May 12
Friday, 18 May 12
Friday, 18 May 12
1 public class Mutable {
             2     private List<Sheep> herd;
             3
             4     public List<Sheep> getHerd() {
             5         return herd;
             6     }
             7 }




Friday, 18 May 12
1 public class Immutable {
            2     private final List<Sheep> herd;
            3
            4     public List<Sheep> getHerd() {
            5         return Collections.unmodifiableList(herd);
            6     }
            7
            8 }




Friday, 18 May 12
Referential
                    Transparency


Friday, 18 May 12
http://www.flickr.com/photos/cruadin/298374640/sizes/l/in/photostream/                  http://www.flickr.com/photos/jsutcliffe/5916342650/sizes/l/in/photostream/
             By Cstaffa (Own work) [GFDL (http://www.gnu.org/copyleft/fdl.html), CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0/) or FAL],
                                                                   via Wikimedia Commons
Friday, 18 May 12
http://www.flickr.com/photos/cruadin/298374640/sizes/l/in/photostream/                  http://www.flickr.com/photos/jsutcliffe/5916342650/sizes/l/in/photostream/
             By Cstaffa (Own work) [GFDL (http://www.gnu.org/copyleft/fdl.html), CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0/) or FAL],
                                                                   via Wikimedia Commons
Friday, 18 May 12
http://www.flickr.com/photos/cruadin/298374640/sizes/l/in/photostream/                  http://www.flickr.com/photos/jsutcliffe/5916342650/sizes/l/in/photostream/
             By Cstaffa (Own work) [GFDL (http://www.gnu.org/copyleft/fdl.html), CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0/) or FAL],
                                                                   via Wikimedia Commons
Friday, 18 May 12
http://www.flickr.com/photos/cradlehall/3583049156/sizes/l/in/photostream/
http://www.flickr.com/photos/cruadin/298374640/sizes/l/in/photostream/                  http://www.flickr.com/photos/jsutcliffe/5916342650/sizes/l/in/photostream/
             By Cstaffa (Own work) [GFDL (http://www.gnu.org/copyleft/fdl.html), CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0/) or FAL],
                                                                   via Wikimedia Commons
Friday, 18 May 12
http://www.flickr.com/photos/cradlehall/3583049156/sizes/l/in/photostream/
http://www.flickr.com/photos/cruadin/298374640/sizes/l/in/photostream/                  http://www.flickr.com/photos/jsutcliffe/5916342650/sizes/l/in/photostream/
             By Cstaffa (Own work) [GFDL (http://www.gnu.org/copyleft/fdl.html), CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0/) or FAL],
                                                                   via Wikimedia Commons
Friday, 18 May 12
http://www.flickr.com/photos/cradlehall/3583049156/sizes/l/in/photostream/
http://www.flickr.com/photos/cruadin/298374640/sizes/l/in/photostream/                  http://www.flickr.com/photos/jsutcliffe/5916342650/sizes/l/in/photostream/
             By Cstaffa (Own work) [GFDL (http://www.gnu.org/copyleft/fdl.html), CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0/) or FAL],
                                                                   via Wikimedia Commons
Friday, 18 May 12
http://www.flickr.com/photos/cradlehall/3583049156/sizes/l/in/photostream/
http://www.flickr.com/photos/cruadin/298374640/sizes/l/in/photostream/                  http://www.flickr.com/photos/jsutcliffe/5916342650/sizes/l/in/photostream/
             By Cstaffa (Own work) [GFDL (http://www.gnu.org/copyleft/fdl.html), CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0/) or FAL],
                                                                   via Wikimedia Commons
Friday, 18 May 12
First Class Functions



Friday, 18 May 12
http://www.flickr.com/photos/22748341@N00/2789592060/sizes/l/in/photostream/




Friday, 18 May 12
1 function shearHerd (herd) {
            2     for (var sheep in herd) {
            3         shear(sheep);
            4     }
            5 }
            6
            7 shearHerd(sheepHerd);




Friday, 18 May 12
http://www.flickr.com/photos/amypalko/3607158016/




Friday, 18 May 12
http://www.flickr.com/photos/emuphoto/206196733




Friday, 18 May 12
1       function map (herd, action) {
            2           for (var animal in herd) {
            3               action(animal);
            4           }
            5       }
            6
            7       map(sheepHerd, shear);
            8       map(cowHerd, milk);




Friday, 18 May 12
Examples



Friday, 18 May 12
LZW in C



Friday, 18 May 12
1   #include    <stdio.h>
      2   #include    <stdlib.h>                                                                67       ushort prev, back;
      3   #include    <string.h>                                                                68       byte c;
      4   #include    <stdint.h>                                                                69   } lzw_dec_t;
      5   #include    <unistd.h>                                                                70
      6   #include    <fcntl.h>                                                                 71   byte* lzw_encode(byte *in, int max_bits)
      7   #include    <sys/types.h>                                                             72   {
      8   #include    <sys/stat.h>                                                              73       int len = _len(in), bits = 9, next_shift = 512;
      9                                                                                         74       ushort code, c, nc, next_code = M_NEW;
     10   /* -------- aux stuff ---------- */                                                   75       lzw_enc_t *d = _new(lzw_enc_t, 512);
     11   void* mem_alloc(size_t item_size, size_t n_item)                                      76
     12   {                                                                                     77       if (max_bits > 16) max_bits = 16;
     13       size_t *x = calloc(1, sizeof(size_t)*2 + n_item * item_size);                     78       if (max_bits < 9 ) max_bits = 12;
     14       x[0] = item_size;                                                                 79
     15       x[1] = n_item;                                                                    80       byte *out = _new(ushort, 4);
     16       return x + 2;                                                                     81       int out_len = 0, o_bits = 0;
     17   }                                                                                     82       uint32_t tmp = 0;
     18                                                                                         83
     19   void* mem_extend(void *m, size_t new_n)                                               84       inline void write_bits(ushort x) {
     20   {                                                                                     85           tmp = (tmp << bits) | x;
     21       size_t *x = (size_t*)m - 2;                                                       86           o_bits += bits;
     22       x = realloc(x, sizeof(size_t) * 2 + *x * new_n);                                  87           if (_len(out) <= out_len) _extend(out);
     23       if (new_n > x[1])                                                                 88           while (o_bits >= 8) {
     24           memset((char*)(x + 2) + x[0] * x[1], 0, x[0] * (new_n - x[1]));               89               o_bits -= 8;
     25       x[1] = new_n;                                                                     90               out[out_len++] = tmp >> o_bits;
     26       return x + 2;                                                                     91               tmp &= (1 << o_bits) - 1;
     27   }                                                                                     92           }
     28                                                                                         93       }
     29   inline void _clear(void *m)                                                           94
     30   {                                                                                     95       //write_bits(M_CLR);
     31       size_t *x = (size_t*)m - 2;                                                       96       for (code = *(in++); --len; ) {
     32       memset(m, 0, x[0] * x[1]);                                                        97           c = *(in++);
     33   }                                                                                     98           if ((nc = d[code].next[c]))
     34                                                                                         99               code = nc;
     35   #define    _new(type, n)   mem_alloc(sizeof(type), n)                                100           else {
     36   #define    _del(m)     { free((size_t*)(m) - 2); m = 0; }                            101               write_bits(code);
     37   #define    _len(m)     *((size_t*)m - 1)                                             102               nc = d[code].next[c] = next_code++;
     38   #define    _setsize(m, n) m = mem_extend(m, n)                                       103               code = c;
     39   #define    _extend(m) m = mem_extend(m, _len(m) * 2)                                 104           }
     40                                                                                        105
     41                                                                                        106           /* next new code would be too long for current table */
     42   /* ----------- LZW stuff -------------- */                                           107           if (next_code == next_shift) {
     43   typedef uint8_t byte;                                                                108               /* either reset table back to 9 bits */
     44   typedef uint16_t ushort;                                                             109               if (++bits > max_bits) {
     45                                                                                        110                   /* table clear marker must occur before bit reset */
     46   #define M_CLR      256 /* clear table marker */                                      111                   write_bits(M_CLR);
     47   #define M_EOD      257 /* end-of-data marker */                                      112
     48   #define M_NEW      258 /* new code index */                                          113                      bits = 9;
     49                                                                                        114                      next_shift = 512;
     50   /* encode and decode dictionary structures.                                          115                      next_code = M_NEW;
     51      for encoding, entry at code index is a list of indices that follow current one,   116                      _clear(d);
     52      i.e. if code 97 is 'a', code 387 is 'ab', and code 1022 is 'abc',                 117                  } else /* or extend table */
     53      then dict[97].next['b'] = 387, dict[387].next['c'] = 1022, etc. */                118                      _setsize(d, next_shift *= 2);
     54   typedef struct {                                                                     119           }
     55       ushort next[256];                                                                120       }
     56   } lzw_enc_t;                                                                         121
     57                                                                                        122       write_bits(code);
     58   /* for decoding, dictionary contains index of whatever prefix index plus trailing    123       write_bits(M_EOD);
     59       byte. i.e. like previous example,                                                124       if (tmp) write_bits(tmp);
     60        dict[1022] = { c: 'c', prev: 387 },                                             125
     61        dict[387] = { c: 'b', prev: 97 },                                               126       _del(d);
     62        dict[97]   = { c: 'a', prev: 0 }                                                127
     63       the "back" element is used for temporarily chaining indices when resolving       128       _setsize(out, out_len);
     64       a code to bytes                                                                  129       return out;
     65    */                                                                                  130   }
     66   typedef struct {



Friday, 18 May 12
LZW in Clojure



Friday, 18 May 12
1 (defn make-dict []
         2   (let [vals (range 0 256)]
         3     (zipmap (map (comp #'list #'char) vals) vals)))
         4
         5 (defn compress [#^String text]
         6   (loop [t (seq text)
         7          r '()
         8          w '()
         9          dict (make-dict)
        10          s 256]
        11     (let [c (first t)]
        12       (if c
        13         (let [wc (cons c w)]
        14           (if (get dict wc)
        15             (recur (rest t) r wc dict s)
        16             (recur (rest t) (cons (get dict w) r) (list c)
        17                (assoc dict wc s) (inc s))))
        18         (reverse (if w (cons (get dict w) r) r))))))
        19
        20 (compress "TOBEORNOTTOBEORTOBEORNOT")




Friday, 18 May 12
LZW in Haskell



Friday, 18 May 12
1        import Data.List
           2        import Data.Char
           3        import Data.Maybe
           4
           5        doLZW _ [] = []
           6        doLZW as (x:xs) = lzw (map return as) [x] xs
           7           where lzw a w [] = [fromJust $ elemIndex w a]
           8                 lzw a w (x:xs) | w' `elem` a = lzw a w' xs
           9                                 | otherwise   = fromJust (elemIndex w a) :
          10                                                            lzw (a++[w']) [x] xs
          11                      where w' = w++[x]




Friday, 18 May 12
Friday, 18 May 12
Recommended Reading



Friday, 18 May 12
Friday, 18 May 12
Closing Thought
                     One thing that makes a programming language ‘more
                    powerful’ in my opinion is the provision of more ways
                      to factor programs. Or if you prefer, more axes of
                        composition. The more different ways you can
                       compose programs out of subprograms, the more
                                    powerful a language is.

                     Reg Braithwaite - Functional Programming Matters




Friday, 18 May 12
Thanks!
                    Andrew Jones @andrew_jones
                     Thom Leggett @thomleggett




Friday, 18 May 12

Contenu connexe

Dernier

MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 

Dernier (20)

MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 

En vedette

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

En vedette (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Functional Programming

  • 1. Functional Programming Friday, 18 May 12
  • 2. Andrew Jones @andrew_jones Thom Leggett @thomleggett Friday, 18 May 12
  • 3. BrisFunctional @brisfunctional brisfunctional.github.com Friday, 18 May 12
  • 6. Noughts and Crosses AI Friday, 18 May 12
  • 11. What is Functional Programming? Friday, 18 May 12
  • 13. Disfunctional Functional Friday, 18 May 12
  • 14. Disfunctional Functional C# Friday, 18 May 12
  • 15. Disfunctional Functional C# Friday, 18 May 12
  • 16. Disfunctional Functional C# Haskell Friday, 18 May 12
  • 17. Disfunctional Functional C# Haskell Javascript Friday, 18 May 12
  • 18. Disfunctional Functional C# Haskell Javascript C Friday, 18 May 12
  • 19. Disfunctional Functional C# Haskell Pascal Javascript C Friday, 18 May 12
  • 20. Disfunctional Java Functional C# Haskell Pascal Javascript C Friday, 18 May 12
  • 21. Disfunctional Java Functional C# Haskell Pascal Javascript C Erlang Friday, 18 May 12
  • 22. Disfunctional Java Functional C# Haskell Pascal Javascript Clojure C Erlang Friday, 18 May 12
  • 23. Disfunctional Java SQL Functional C# Haskell Pascal Javascript Clojure C Erlang Friday, 18 May 12
  • 24. Python Disfunctional Java SQL Functional C# Haskell Pascal Javascript Clojure C Erlang Friday, 18 May 12
  • 25. Ruby Python Disfunctional Java SQL Functional C# Haskell Pascal Javascript Clojure C Erlang Friday, 18 May 12
  • 26. Ruby Python Disfunctional Java SQL Functional C# Haskell Pascal Javascript Clojure C PHP Erlang Friday, 18 May 12
  • 31. 1 public class Mutable { 2 private List<Sheep> herd; 3 4 public List<Sheep> getHerd() { 5 return herd; 6 } 7 } Friday, 18 May 12
  • 32. 1 public class Immutable { 2 private final List<Sheep> herd; 3 4 public List<Sheep> getHerd() { 5 return Collections.unmodifiableList(herd); 6 } 7 8 } Friday, 18 May 12
  • 33. Referential Transparency Friday, 18 May 12
  • 34. http://www.flickr.com/photos/cruadin/298374640/sizes/l/in/photostream/ http://www.flickr.com/photos/jsutcliffe/5916342650/sizes/l/in/photostream/ By Cstaffa (Own work) [GFDL (http://www.gnu.org/copyleft/fdl.html), CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0/) or FAL], via Wikimedia Commons Friday, 18 May 12
  • 35. http://www.flickr.com/photos/cruadin/298374640/sizes/l/in/photostream/ http://www.flickr.com/photos/jsutcliffe/5916342650/sizes/l/in/photostream/ By Cstaffa (Own work) [GFDL (http://www.gnu.org/copyleft/fdl.html), CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0/) or FAL], via Wikimedia Commons Friday, 18 May 12
  • 36. http://www.flickr.com/photos/cruadin/298374640/sizes/l/in/photostream/ http://www.flickr.com/photos/jsutcliffe/5916342650/sizes/l/in/photostream/ By Cstaffa (Own work) [GFDL (http://www.gnu.org/copyleft/fdl.html), CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0/) or FAL], via Wikimedia Commons Friday, 18 May 12
  • 37. http://www.flickr.com/photos/cradlehall/3583049156/sizes/l/in/photostream/ http://www.flickr.com/photos/cruadin/298374640/sizes/l/in/photostream/ http://www.flickr.com/photos/jsutcliffe/5916342650/sizes/l/in/photostream/ By Cstaffa (Own work) [GFDL (http://www.gnu.org/copyleft/fdl.html), CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0/) or FAL], via Wikimedia Commons Friday, 18 May 12
  • 38. http://www.flickr.com/photos/cradlehall/3583049156/sizes/l/in/photostream/ http://www.flickr.com/photos/cruadin/298374640/sizes/l/in/photostream/ http://www.flickr.com/photos/jsutcliffe/5916342650/sizes/l/in/photostream/ By Cstaffa (Own work) [GFDL (http://www.gnu.org/copyleft/fdl.html), CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0/) or FAL], via Wikimedia Commons Friday, 18 May 12
  • 39. http://www.flickr.com/photos/cradlehall/3583049156/sizes/l/in/photostream/ http://www.flickr.com/photos/cruadin/298374640/sizes/l/in/photostream/ http://www.flickr.com/photos/jsutcliffe/5916342650/sizes/l/in/photostream/ By Cstaffa (Own work) [GFDL (http://www.gnu.org/copyleft/fdl.html), CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0/) or FAL], via Wikimedia Commons Friday, 18 May 12
  • 40. http://www.flickr.com/photos/cradlehall/3583049156/sizes/l/in/photostream/ http://www.flickr.com/photos/cruadin/298374640/sizes/l/in/photostream/ http://www.flickr.com/photos/jsutcliffe/5916342650/sizes/l/in/photostream/ By Cstaffa (Own work) [GFDL (http://www.gnu.org/copyleft/fdl.html), CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0/) or FAL], via Wikimedia Commons Friday, 18 May 12
  • 43. 1 function shearHerd (herd) { 2 for (var sheep in herd) { 3 shear(sheep); 4 } 5 } 6 7 shearHerd(sheepHerd); Friday, 18 May 12
  • 46. 1 function map (herd, action) { 2 for (var animal in herd) { 3 action(animal); 4 } 5 } 6 7 map(sheepHerd, shear); 8 map(cowHerd, milk); Friday, 18 May 12
  • 48. LZW in C Friday, 18 May 12
  • 49. 1 #include <stdio.h> 2 #include <stdlib.h> 67 ushort prev, back; 3 #include <string.h> 68 byte c; 4 #include <stdint.h> 69 } lzw_dec_t; 5 #include <unistd.h> 70 6 #include <fcntl.h> 71 byte* lzw_encode(byte *in, int max_bits) 7 #include <sys/types.h> 72 { 8 #include <sys/stat.h> 73 int len = _len(in), bits = 9, next_shift = 512; 9 74 ushort code, c, nc, next_code = M_NEW; 10 /* -------- aux stuff ---------- */ 75 lzw_enc_t *d = _new(lzw_enc_t, 512); 11 void* mem_alloc(size_t item_size, size_t n_item) 76 12 { 77 if (max_bits > 16) max_bits = 16; 13 size_t *x = calloc(1, sizeof(size_t)*2 + n_item * item_size); 78 if (max_bits < 9 ) max_bits = 12; 14 x[0] = item_size; 79 15 x[1] = n_item; 80 byte *out = _new(ushort, 4); 16 return x + 2; 81 int out_len = 0, o_bits = 0; 17 } 82 uint32_t tmp = 0; 18 83 19 void* mem_extend(void *m, size_t new_n) 84 inline void write_bits(ushort x) { 20 { 85 tmp = (tmp << bits) | x; 21 size_t *x = (size_t*)m - 2; 86 o_bits += bits; 22 x = realloc(x, sizeof(size_t) * 2 + *x * new_n); 87 if (_len(out) <= out_len) _extend(out); 23 if (new_n > x[1]) 88 while (o_bits >= 8) { 24 memset((char*)(x + 2) + x[0] * x[1], 0, x[0] * (new_n - x[1])); 89 o_bits -= 8; 25 x[1] = new_n; 90 out[out_len++] = tmp >> o_bits; 26 return x + 2; 91 tmp &= (1 << o_bits) - 1; 27 } 92 } 28 93 } 29 inline void _clear(void *m) 94 30 { 95 //write_bits(M_CLR); 31 size_t *x = (size_t*)m - 2; 96 for (code = *(in++); --len; ) { 32 memset(m, 0, x[0] * x[1]); 97 c = *(in++); 33 } 98 if ((nc = d[code].next[c])) 34 99 code = nc; 35 #define _new(type, n) mem_alloc(sizeof(type), n) 100 else { 36 #define _del(m) { free((size_t*)(m) - 2); m = 0; } 101 write_bits(code); 37 #define _len(m) *((size_t*)m - 1) 102 nc = d[code].next[c] = next_code++; 38 #define _setsize(m, n) m = mem_extend(m, n) 103 code = c; 39 #define _extend(m) m = mem_extend(m, _len(m) * 2) 104 } 40 105 41 106 /* next new code would be too long for current table */ 42 /* ----------- LZW stuff -------------- */ 107 if (next_code == next_shift) { 43 typedef uint8_t byte; 108 /* either reset table back to 9 bits */ 44 typedef uint16_t ushort; 109 if (++bits > max_bits) { 45 110 /* table clear marker must occur before bit reset */ 46 #define M_CLR 256 /* clear table marker */ 111 write_bits(M_CLR); 47 #define M_EOD 257 /* end-of-data marker */ 112 48 #define M_NEW 258 /* new code index */ 113 bits = 9; 49 114 next_shift = 512; 50 /* encode and decode dictionary structures. 115 next_code = M_NEW; 51 for encoding, entry at code index is a list of indices that follow current one, 116 _clear(d); 52 i.e. if code 97 is 'a', code 387 is 'ab', and code 1022 is 'abc', 117 } else /* or extend table */ 53 then dict[97].next['b'] = 387, dict[387].next['c'] = 1022, etc. */ 118 _setsize(d, next_shift *= 2); 54 typedef struct { 119 } 55 ushort next[256]; 120 } 56 } lzw_enc_t; 121 57 122 write_bits(code); 58 /* for decoding, dictionary contains index of whatever prefix index plus trailing 123 write_bits(M_EOD); 59 byte. i.e. like previous example, 124 if (tmp) write_bits(tmp); 60 dict[1022] = { c: 'c', prev: 387 }, 125 61 dict[387] = { c: 'b', prev: 97 }, 126 _del(d); 62 dict[97] = { c: 'a', prev: 0 } 127 63 the "back" element is used for temporarily chaining indices when resolving 128 _setsize(out, out_len); 64 a code to bytes 129 return out; 65 */ 130 } 66 typedef struct { Friday, 18 May 12
  • 51. 1 (defn make-dict [] 2 (let [vals (range 0 256)] 3 (zipmap (map (comp #'list #'char) vals) vals))) 4 5 (defn compress [#^String text] 6 (loop [t (seq text) 7 r '() 8 w '() 9 dict (make-dict) 10 s 256] 11 (let [c (first t)] 12 (if c 13 (let [wc (cons c w)] 14 (if (get dict wc) 15 (recur (rest t) r wc dict s) 16 (recur (rest t) (cons (get dict w) r) (list c) 17 (assoc dict wc s) (inc s)))) 18 (reverse (if w (cons (get dict w) r) r)))))) 19 20 (compress "TOBEORNOTTOBEORTOBEORNOT") Friday, 18 May 12
  • 53. 1 import Data.List 2 import Data.Char 3 import Data.Maybe 4 5 doLZW _ [] = [] 6 doLZW as (x:xs) = lzw (map return as) [x] xs 7 where lzw a w [] = [fromJust $ elemIndex w a] 8 lzw a w (x:xs) | w' `elem` a = lzw a w' xs 9 | otherwise = fromJust (elemIndex w a) : 10 lzw (a++[w']) [x] xs 11 where w' = w++[x] Friday, 18 May 12
  • 57. Closing Thought One thing that makes a programming language ‘more powerful’ in my opinion is the provision of more ways to factor programs. Or if you prefer, more axes of composition. The more different ways you can compose programs out of subprograms, the more powerful a language is. Reg Braithwaite - Functional Programming Matters Friday, 18 May 12
  • 58. Thanks! Andrew Jones @andrew_jones Thom Leggett @thomleggett Friday, 18 May 12