SlideShare ist ein Scribd-Unternehmen logo
1 von 47
DataWeave 2.0
Language Fundamentals
Joshua Erney
● Mountain State Software Solutions
● www.jerney.io
● www.linkedin.com/in/jerney
Agenda
1. Introduction
2. What is DataWeave?
3. Language Fundamentals
a. Expressions
b. Immutable Data
c. Functions
4. Learning Tips
5. Practical Tips
6. Additional Info
7. Questions?
● Joshua Erney
● Blog @ www.jerney.io
● Working at MS3 as a Software Dev for almost 4 yrs (programming ~9 yrs), BA before that
● Love programming languages
● Other interests: business, personal finance, Super Smash Bros
Who Am I?
What is DataWeave?
● Domain-specific Language created specifically to optimize data transformations regardless of
source and target formats
● Fully-fledged programming language
● Adheres to the core tenets of functional programming
Why Should You Learn DataWeave?
● Don’t need to concern yourself with parsing and (de)serialization of data
● Less lines of code needed to perform data transformations (Less bugs)
● The programming concepts you must learn to effectively use DataWeave will make you a better
programmer. The goal of this talk is to give you an introductory understanding of these concepts.
Why not Java?
● Scope is massive (general purpose programming language)
● Object-Oriented: great for designing large-scale solutions (like the Mule runtime), not great for
expressing small algorithms like data transformations
● Supports Functional programming (Java 8 Streams), but rather reluctantly
● Verbose
Language Fundamentals
If you’re only going to learn one thing from this
webinar is should be this...
DataWeave is a functional
programming language.
What is Functional Programming?
● Programming paradigm, like “procedural” or “object-oriented”
● Wikipedia says:
“... a programming paradigm … that treats computation as the evaluation of mathematical
functions and avoids changing-state and mutable data” (emphasis mine)
and
“It is a declarative programming paradigm, which means programming is done with expressions
and declarations instead of statements.” (emphasis mine, again)
What is meant by “mathematical functions”:
A functions is deterministic, the same input
ALWAYS yields the same output:
fun add(x, y) = x + y
add(1, 1) // = 2
add(1, 1) // = 2
… // for eternity this will be 2
This is different from methods, which can modify
the state of the object with which they’re
associated:
Adder one = new Adder(1);
one.add(1) // = 2
onw.setValue(2)
one.add(1) // = 3 ???
Core Concepts (Language Fundamentals)
“... a programming paradigm … that treats computation as the evaluation of mathematical
functions and avoids changing state and mutable data” (emphasis mine)
“It is a declarative programming paradigm, which means programming is done with expressions
and declarations instead of statements.” (emphasis mine, again)
1. Expressions Instead of Statements
2. Immutable Data
3. Mathematical Functions
Expressions
(Most) Everything is an Expression
Most programs are made up of expressions.
● An expression is something that evaluates to a value, e.g, 2 + 2
● A statement is more like a command, and represents an action, e.g., System.out.print(“Hello,
World!);
In other words: Expressions return values, statements do not.
Statements are great for doing things like printing, saving and setting values, and I/O operations.
Expressions are great for doing calculations and transformations
Everything is an Expression (cont.)
Java Statements
boolean even = true;
int x = 0;
if(even) {
x = 2;
} else {
x = 1;
}
DataWeave Expressions
var even = true
var x = if (even) 2 else 1
This returns
This does not return
Expressions Can Always Be Substituted
fun complexPokemonStuff(x): Array = ...
---
complexPokemonStuff(payload) map $.pickachu
USE THIS WHEN YOU’RE DEBUGGING
Expressions Can Always Be Substituted
fun complexPokemonStuff(x): Array = ...
---
[{“pickachu”: “electric”, “charizard”: “fire”}] map $.pickachu
USE THIS WHEN YOU’RE DEBUGGING
Key Point 🔑
Statements in DataWeave are rare, so you can’t perform an action that doesn’t return something (e.g.
simply writing to the console). An exception to this is declarations (vars and funs) and some
dw::Runtime functions.
Every logical grouping of DataWeave code returns something. Expressions can always be replaced by the
value they return. Use this property when you’re debugging!
Immutable Data
Objects are Immutable, No Changing State
● Once an object is created, it never changes
● Typically, this is an advantage, because you have WAY less to keep track of
● How does any work get done?
Is the payload modified?
No, so what’s happening instead?
The payload “variable” is being reassigned, like this:
// Psuedo Code
payload = http.getRequestData()
// New payload created from old payload, old payload reassigned to new payload
payload = transformMessage(payload)
And this happens to the target of every DataWeave transform. If the target is a variable, and the variable is not
assigned yet, it is assigned. If the variable is already assigned, it is reassigned to the output of the DataWeave
script.
Mental Model
Mental Model
Key Point 🔑
Always remember that you are never modifying anything when you use DataWeave code. DataWeave
cannot modify values, it can only create new ones. It gives the “illusion” of modification through
reassignment.
Functions
Functions
● The main means of computation in Functional Programming
Functions (cont.)
● The main means of computation in Functional Programming
● Functions read the input, and produce output according to a set of rules (the function body)
● In general, given the same input, DW functions will always return the same output
Functions (cont.)
Functions return the last expression in the body that is evaluated:
fun addDamage(currentHealth, attackDamage) = currentHealth - attackDamage
A consequence of this is there is no return keyword
First Class Functions
DataWeave Supports Higher-Order Functions
● Fancy way of saying: functions that take other functions as inputs, or produce functions as output
● You do it all the time with map, filter, groupBy, etc.
payload map (employee) -> employee.id
This is a function passed to the `map` function
Understanding Higher-Order Functions
Higher-order functions are very good at separating concerns. Using our map example from earlier… map
takes care of looping through the input array and applying the function to each value. The function
passed to map describes how each element in the input array should be transformed.
payload map (employee) -> employee.id
Takes care of the iteration and applying the function
Describes how each element should be transformed
Did I Mention… Functions?
Named functions:
fun greeting(name) = “Hello, $(name)”
Unnamed functions (aka “lambdas”):
(name) -> “Hello, $(name)”
Function assignment is just syntax sugar for lambdas assigned to a variable:
var greeting = (name) -> “Hello, $(name)”
greeting(“Pickachu!”)
Did I Mention… Functions? (cont.)
Without Lambdas
fun mapping(pok) =
{name: pok.name, lvl: pok.lvl + 1}
---
payload map mapping($)
With Lambdas
payload map (pokemon) -> {
name: pokemon.name,
lvl: pokemon.lvl
}
Lambdas make it easy to use higher order functions. We don’t need to use as much code.
Key Point 🔑
Functions are DataWeave’s most powerful and flexible tool for building transformations. They allow us
to cleanly separate concerns (especially in addition to Modules), and compose complex behavior out of
simple behavior.
Learning Tips
Settle in for the journey
1. Accept that DataWeave is a complete programming language
2. Accept that DataWeave is a functional programming language, and you might not know anything
about functional programming, yet
3. Accept that there is not a lot of content out there that discusses functional programming
principles as they relate to DataWeave, or how DataWeave is a functional programming language
(except my blog 😉)
Functions are King
● Make sure you fully understand what “pure” (i.e. mathematical) functions are, and how they’re
beneficial to this style of programming (transformation)
● Make sure you understand that when you’re using map, filter, etc, that you’re using higher-
order functions
● Finally, try writing some higher order functions of your own
Don’t Give Up!
● The paradigm shift from object-oriented to functional is difficult
● Takes time and deliberate practice
● If it doesn’t work with DataWeave, try practicing with a different language:
○ Scala
○ JavaScript (some parts, search “Functional JavaScript”)
○ Clojure
● Ask questions on the Mule Forums (to get help from everyone besides me), Stack Overflow (to get
help from me and some other people)
● Reach out to me if I can help
Practical Tips
Pattern Matching
if (s == “pikachu”)
“Pika pika”
else if (s == “bulbasaur”)
“Bulba bulba”
else
“”
s match {
case “pikachu” -> “Pika pika”
case “bulbasaur” -> “Bulba bulba”
else -> “”
}
Use pattern matching to eliminate nested if/else expressions if possible:
Using “do” Expressions
finallyDoThis(
thenDoThis(
firstDoThis(
calculate(x),
calculate(x))))
do {
var calc = calculate(x)
var res1 = firstDoThis(calc, calc)
var res2 = thenDoThis(res1)
---
finallyDoThis(res2)
}
Use “do” expressions to prevent calculating a value multiple times, and reduce the nesting of function
calls, increasing readability
Creating Reuseable Functions
● Try to create functions that abstract over data types and not the data itself
"It is better to have 100 functions operate on one data structure than 10 functions on 10 data
structures." —Alan Perlis
Function Composition
● Small functions
● Wrap groupings of small functions in another function
● This is composition
● Promotes reuse
Additional Info
Additional Info:
Follow me on twitter: @jerney_io
Learn how to use reduce (good content on HOFs as well): https://www.jerney.io/dataweave-when-to-use-
reduce/
Looking for something more advanced? (pattern matching, recursion): https://www.jerney.io/dataweave-
applywhenkey-function/, https://www.jerney.io/how-to-modify-all-values-of-an-element/
Additional Info (Cont.)
Videos are your thing? Check out this great talk on Functional Programming by Russ Olsen:
https://www.youtube.com/watch?v=0if71HOyVjY
Good Intro to Functional Programming: First 6 Chapters of “On Lisp”, by Graham:
http://ep.yimg.com/ty/cdn/paulgraham/onlisp.pdf
Another Good Intro: First 2 Chapters of “Structure and Interpretation of Computer Programs”, by
Abelson, Sussman & Sussman: http://web.mit.edu/alexmv/6.037/sicp.pdf
Something way more advanced: “Functional Programming in Scala” by Chiusana & Bjarnason:
https://github.com/awantik/scala-
programming/blob/master/Manning%20Functional%20Programming%20in%20Scala%20(2015).pdf
Questions?

Weitere ähnliche Inhalte

Was ist angesagt?

Go faster with_native_compilation Part-2
Go faster with_native_compilation Part-2Go faster with_native_compilation Part-2
Go faster with_native_compilation Part-2Rajeev Rastogi (KRR)
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object ModelWebStackAcademy
 
Java Script Language Tutorial
Java Script Language TutorialJava Script Language Tutorial
Java Script Language Tutorialvikram singh
 
Operator Overloading and Scope of Variable
Operator Overloading and Scope of VariableOperator Overloading and Scope of Variable
Operator Overloading and Scope of VariableMOHIT DADU
 
Scala: functional programming for the imperative mind
Scala: functional programming for the imperative mindScala: functional programming for the imperative mind
Scala: functional programming for the imperative mindSander Mak (@Sander_Mak)
 
Placement and variable 03 (js)
Placement and variable 03 (js)Placement and variable 03 (js)
Placement and variable 03 (js)AbhishekMondal42
 
DIWE - Working with MySQL Databases
DIWE - Working with MySQL DatabasesDIWE - Working with MySQL Databases
DIWE - Working with MySQL DatabasesRasan Samarasinghe
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdfHiroshi Ono
 
Introduction to objective c
Introduction to objective cIntroduction to objective c
Introduction to objective cMayank Jalotra
 
A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++M Hussnain Ali
 
Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Jonas Bonér
 

Was ist angesagt? (20)

Go faster with_native_compilation Part-2
Go faster with_native_compilation Part-2Go faster with_native_compilation Part-2
Go faster with_native_compilation Part-2
 
Mule mel 3
Mule mel 3Mule mel 3
Mule mel 3
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
 
Go Faster With Native Compilation
Go Faster With Native CompilationGo Faster With Native Compilation
Go Faster With Native Compilation
 
Java Script Language Tutorial
Java Script Language TutorialJava Script Language Tutorial
Java Script Language Tutorial
 
C++ classes
C++ classesC++ classes
C++ classes
 
Operator Overloading and Scope of Variable
Operator Overloading and Scope of VariableOperator Overloading and Scope of Variable
Operator Overloading and Scope of Variable
 
Chapter 2 java
Chapter 2 javaChapter 2 java
Chapter 2 java
 
Scala: functional programming for the imperative mind
Scala: functional programming for the imperative mindScala: functional programming for the imperative mind
Scala: functional programming for the imperative mind
 
camel-scala.pdf
camel-scala.pdfcamel-scala.pdf
camel-scala.pdf
 
Placement and variable 03 (js)
Placement and variable 03 (js)Placement and variable 03 (js)
Placement and variable 03 (js)
 
Inheritance
InheritanceInheritance
Inheritance
 
DIWE - Working with MySQL Databases
DIWE - Working with MySQL DatabasesDIWE - Working with MySQL Databases
DIWE - Working with MySQL Databases
 
Object-Oriented Programming Using C++
Object-Oriented Programming Using C++Object-Oriented Programming Using C++
Object-Oriented Programming Using C++
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 
Introduction to objective c
Introduction to objective cIntroduction to objective c
Introduction to objective c
 
A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++
 
Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)
 
Introduction to c ++ part -2
Introduction to c ++   part -2Introduction to c ++   part -2
Introduction to c ++ part -2
 
LINQ
LINQLINQ
LINQ
 

Ähnlich wie DataWeave 2.0 Language Fundamentals

Data weave 2.0 language fundamentals
Data weave 2.0 language fundamentalsData weave 2.0 language fundamentals
Data weave 2.0 language fundamentalsManjuKumara GH
 
Functional Swift
Functional SwiftFunctional Swift
Functional SwiftGeison Goes
 
DataWeave 2.0 - MuleSoft CONNECT 2019
DataWeave 2.0 - MuleSoft CONNECT 2019DataWeave 2.0 - MuleSoft CONNECT 2019
DataWeave 2.0 - MuleSoft CONNECT 2019Sabrina Marechal
 
Basics of Programming - A Review Guide
Basics of Programming - A Review GuideBasics of Programming - A Review Guide
Basics of Programming - A Review GuideBenjamin Kissinger
 
Programming picaresque
Programming picaresqueProgramming picaresque
Programming picaresqueBret McGuire
 
Go Beyond Higher Order Functions: A Journey into Functional Programming
Go Beyond Higher Order Functions: A Journey into Functional ProgrammingGo Beyond Higher Order Functions: A Journey into Functional Programming
Go Beyond Higher Order Functions: A Journey into Functional ProgrammingLex Sheehan
 
Knowledge of Javascript
Knowledge of JavascriptKnowledge of Javascript
Knowledge of JavascriptSamuel Abraham
 
Code Cleanup: A Data Scientist's Guide to Sparkling Code
Code Cleanup: A Data Scientist's Guide to Sparkling CodeCode Cleanup: A Data Scientist's Guide to Sparkling Code
Code Cleanup: A Data Scientist's Guide to Sparkling CodeCorrie Bartelheimer
 
Engineering Student MuleSoft Meetup#6 - Basic Understanding of DataWeave With...
Engineering Student MuleSoft Meetup#6 - Basic Understanding of DataWeave With...Engineering Student MuleSoft Meetup#6 - Basic Understanding of DataWeave With...
Engineering Student MuleSoft Meetup#6 - Basic Understanding of DataWeave With...Jitendra Bafna
 
Fp for the oo programmer
Fp for the oo programmerFp for the oo programmer
Fp for the oo programmerShawn Button
 
20230721_OKC_Meetup_MuleSoft.pptx
20230721_OKC_Meetup_MuleSoft.pptx20230721_OKC_Meetup_MuleSoft.pptx
20230721_OKC_Meetup_MuleSoft.pptxDianeKesler1
 
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)Ovidiu Farauanu
 
Modern_2.pptx for java
Modern_2.pptx for java Modern_2.pptx for java
Modern_2.pptx for java MayaTofik
 
Programming Paradigms
Programming ParadigmsProgramming Paradigms
Programming ParadigmsDirecti Group
 
Software Engineering Best Practices @ Nylas
Software Engineering Best Practices @ NylasSoftware Engineering Best Practices @ Nylas
Software Engineering Best Practices @ NylasBen Gotow
 
(3) cpp procedural programming
(3) cpp procedural programming(3) cpp procedural programming
(3) cpp procedural programmingNico Ludwig
 
Concepts In Object Oriented Programming Languages
Concepts In Object Oriented Programming LanguagesConcepts In Object Oriented Programming Languages
Concepts In Object Oriented Programming Languagesppd1961
 

Ähnlich wie DataWeave 2.0 Language Fundamentals (20)

Data weave 2.0 language fundamentals
Data weave 2.0 language fundamentalsData weave 2.0 language fundamentals
Data weave 2.0 language fundamentals
 
Functional Swift
Functional SwiftFunctional Swift
Functional Swift
 
Functional programming
Functional programmingFunctional programming
Functional programming
 
DataWeave 2.0 - MuleSoft CONNECT 2019
DataWeave 2.0 - MuleSoft CONNECT 2019DataWeave 2.0 - MuleSoft CONNECT 2019
DataWeave 2.0 - MuleSoft CONNECT 2019
 
Basics of Programming - A Review Guide
Basics of Programming - A Review GuideBasics of Programming - A Review Guide
Basics of Programming - A Review Guide
 
Programming picaresque
Programming picaresqueProgramming picaresque
Programming picaresque
 
Go Beyond Higher Order Functions: A Journey into Functional Programming
Go Beyond Higher Order Functions: A Journey into Functional ProgrammingGo Beyond Higher Order Functions: A Journey into Functional Programming
Go Beyond Higher Order Functions: A Journey into Functional Programming
 
Functional Go
Functional GoFunctional Go
Functional Go
 
Knowledge of Javascript
Knowledge of JavascriptKnowledge of Javascript
Knowledge of Javascript
 
Code Cleanup: A Data Scientist's Guide to Sparkling Code
Code Cleanup: A Data Scientist's Guide to Sparkling CodeCode Cleanup: A Data Scientist's Guide to Sparkling Code
Code Cleanup: A Data Scientist's Guide to Sparkling Code
 
Engineering Student MuleSoft Meetup#6 - Basic Understanding of DataWeave With...
Engineering Student MuleSoft Meetup#6 - Basic Understanding of DataWeave With...Engineering Student MuleSoft Meetup#6 - Basic Understanding of DataWeave With...
Engineering Student MuleSoft Meetup#6 - Basic Understanding of DataWeave With...
 
Fp for the oo programmer
Fp for the oo programmerFp for the oo programmer
Fp for the oo programmer
 
20230721_OKC_Meetup_MuleSoft.pptx
20230721_OKC_Meetup_MuleSoft.pptx20230721_OKC_Meetup_MuleSoft.pptx
20230721_OKC_Meetup_MuleSoft.pptx
 
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
 
programming.ppt
programming.pptprogramming.ppt
programming.ppt
 
Modern_2.pptx for java
Modern_2.pptx for java Modern_2.pptx for java
Modern_2.pptx for java
 
Programming Paradigms
Programming ParadigmsProgramming Paradigms
Programming Paradigms
 
Software Engineering Best Practices @ Nylas
Software Engineering Best Practices @ NylasSoftware Engineering Best Practices @ Nylas
Software Engineering Best Practices @ Nylas
 
(3) cpp procedural programming
(3) cpp procedural programming(3) cpp procedural programming
(3) cpp procedural programming
 
Concepts In Object Oriented Programming Languages
Concepts In Object Oriented Programming LanguagesConcepts In Object Oriented Programming Languages
Concepts In Object Oriented Programming Languages
 

Kürzlich hochgeladen

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
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
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
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
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
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
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 

Kürzlich hochgeladen (20)

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
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...
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 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
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
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
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 

DataWeave 2.0 Language Fundamentals

  • 1. DataWeave 2.0 Language Fundamentals Joshua Erney ● Mountain State Software Solutions ● www.jerney.io ● www.linkedin.com/in/jerney
  • 2. Agenda 1. Introduction 2. What is DataWeave? 3. Language Fundamentals a. Expressions b. Immutable Data c. Functions 4. Learning Tips 5. Practical Tips 6. Additional Info 7. Questions?
  • 3. ● Joshua Erney ● Blog @ www.jerney.io ● Working at MS3 as a Software Dev for almost 4 yrs (programming ~9 yrs), BA before that ● Love programming languages ● Other interests: business, personal finance, Super Smash Bros Who Am I?
  • 4. What is DataWeave? ● Domain-specific Language created specifically to optimize data transformations regardless of source and target formats ● Fully-fledged programming language ● Adheres to the core tenets of functional programming
  • 5. Why Should You Learn DataWeave? ● Don’t need to concern yourself with parsing and (de)serialization of data ● Less lines of code needed to perform data transformations (Less bugs) ● The programming concepts you must learn to effectively use DataWeave will make you a better programmer. The goal of this talk is to give you an introductory understanding of these concepts.
  • 6. Why not Java? ● Scope is massive (general purpose programming language) ● Object-Oriented: great for designing large-scale solutions (like the Mule runtime), not great for expressing small algorithms like data transformations ● Supports Functional programming (Java 8 Streams), but rather reluctantly ● Verbose
  • 8. If you’re only going to learn one thing from this webinar is should be this...
  • 9. DataWeave is a functional programming language.
  • 10. What is Functional Programming? ● Programming paradigm, like “procedural” or “object-oriented” ● Wikipedia says: “... a programming paradigm … that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data” (emphasis mine) and “It is a declarative programming paradigm, which means programming is done with expressions and declarations instead of statements.” (emphasis mine, again)
  • 11. What is meant by “mathematical functions”: A functions is deterministic, the same input ALWAYS yields the same output: fun add(x, y) = x + y add(1, 1) // = 2 add(1, 1) // = 2 … // for eternity this will be 2 This is different from methods, which can modify the state of the object with which they’re associated: Adder one = new Adder(1); one.add(1) // = 2 onw.setValue(2) one.add(1) // = 3 ???
  • 12. Core Concepts (Language Fundamentals) “... a programming paradigm … that treats computation as the evaluation of mathematical functions and avoids changing state and mutable data” (emphasis mine) “It is a declarative programming paradigm, which means programming is done with expressions and declarations instead of statements.” (emphasis mine, again) 1. Expressions Instead of Statements 2. Immutable Data 3. Mathematical Functions
  • 14. (Most) Everything is an Expression Most programs are made up of expressions. ● An expression is something that evaluates to a value, e.g, 2 + 2 ● A statement is more like a command, and represents an action, e.g., System.out.print(“Hello, World!); In other words: Expressions return values, statements do not. Statements are great for doing things like printing, saving and setting values, and I/O operations. Expressions are great for doing calculations and transformations
  • 15. Everything is an Expression (cont.) Java Statements boolean even = true; int x = 0; if(even) { x = 2; } else { x = 1; } DataWeave Expressions var even = true var x = if (even) 2 else 1 This returns This does not return
  • 16. Expressions Can Always Be Substituted fun complexPokemonStuff(x): Array = ... --- complexPokemonStuff(payload) map $.pickachu USE THIS WHEN YOU’RE DEBUGGING
  • 17. Expressions Can Always Be Substituted fun complexPokemonStuff(x): Array = ... --- [{“pickachu”: “electric”, “charizard”: “fire”}] map $.pickachu USE THIS WHEN YOU’RE DEBUGGING
  • 18. Key Point 🔑 Statements in DataWeave are rare, so you can’t perform an action that doesn’t return something (e.g. simply writing to the console). An exception to this is declarations (vars and funs) and some dw::Runtime functions. Every logical grouping of DataWeave code returns something. Expressions can always be replaced by the value they return. Use this property when you’re debugging!
  • 20. Objects are Immutable, No Changing State ● Once an object is created, it never changes ● Typically, this is an advantage, because you have WAY less to keep track of ● How does any work get done?
  • 21. Is the payload modified?
  • 22. No, so what’s happening instead? The payload “variable” is being reassigned, like this: // Psuedo Code payload = http.getRequestData() // New payload created from old payload, old payload reassigned to new payload payload = transformMessage(payload) And this happens to the target of every DataWeave transform. If the target is a variable, and the variable is not assigned yet, it is assigned. If the variable is already assigned, it is reassigned to the output of the DataWeave script.
  • 25. Key Point 🔑 Always remember that you are never modifying anything when you use DataWeave code. DataWeave cannot modify values, it can only create new ones. It gives the “illusion” of modification through reassignment.
  • 27. Functions ● The main means of computation in Functional Programming
  • 28. Functions (cont.) ● The main means of computation in Functional Programming ● Functions read the input, and produce output according to a set of rules (the function body) ● In general, given the same input, DW functions will always return the same output
  • 29. Functions (cont.) Functions return the last expression in the body that is evaluated: fun addDamage(currentHealth, attackDamage) = currentHealth - attackDamage A consequence of this is there is no return keyword
  • 30. First Class Functions DataWeave Supports Higher-Order Functions ● Fancy way of saying: functions that take other functions as inputs, or produce functions as output ● You do it all the time with map, filter, groupBy, etc. payload map (employee) -> employee.id This is a function passed to the `map` function
  • 31. Understanding Higher-Order Functions Higher-order functions are very good at separating concerns. Using our map example from earlier… map takes care of looping through the input array and applying the function to each value. The function passed to map describes how each element in the input array should be transformed. payload map (employee) -> employee.id Takes care of the iteration and applying the function Describes how each element should be transformed
  • 32. Did I Mention… Functions? Named functions: fun greeting(name) = “Hello, $(name)” Unnamed functions (aka “lambdas”): (name) -> “Hello, $(name)” Function assignment is just syntax sugar for lambdas assigned to a variable: var greeting = (name) -> “Hello, $(name)” greeting(“Pickachu!”)
  • 33. Did I Mention… Functions? (cont.) Without Lambdas fun mapping(pok) = {name: pok.name, lvl: pok.lvl + 1} --- payload map mapping($) With Lambdas payload map (pokemon) -> { name: pokemon.name, lvl: pokemon.lvl } Lambdas make it easy to use higher order functions. We don’t need to use as much code.
  • 34. Key Point 🔑 Functions are DataWeave’s most powerful and flexible tool for building transformations. They allow us to cleanly separate concerns (especially in addition to Modules), and compose complex behavior out of simple behavior.
  • 36. Settle in for the journey 1. Accept that DataWeave is a complete programming language 2. Accept that DataWeave is a functional programming language, and you might not know anything about functional programming, yet 3. Accept that there is not a lot of content out there that discusses functional programming principles as they relate to DataWeave, or how DataWeave is a functional programming language (except my blog 😉)
  • 37. Functions are King ● Make sure you fully understand what “pure” (i.e. mathematical) functions are, and how they’re beneficial to this style of programming (transformation) ● Make sure you understand that when you’re using map, filter, etc, that you’re using higher- order functions ● Finally, try writing some higher order functions of your own
  • 38. Don’t Give Up! ● The paradigm shift from object-oriented to functional is difficult ● Takes time and deliberate practice ● If it doesn’t work with DataWeave, try practicing with a different language: ○ Scala ○ JavaScript (some parts, search “Functional JavaScript”) ○ Clojure ● Ask questions on the Mule Forums (to get help from everyone besides me), Stack Overflow (to get help from me and some other people) ● Reach out to me if I can help
  • 40. Pattern Matching if (s == “pikachu”) “Pika pika” else if (s == “bulbasaur”) “Bulba bulba” else “” s match { case “pikachu” -> “Pika pika” case “bulbasaur” -> “Bulba bulba” else -> “” } Use pattern matching to eliminate nested if/else expressions if possible:
  • 41. Using “do” Expressions finallyDoThis( thenDoThis( firstDoThis( calculate(x), calculate(x)))) do { var calc = calculate(x) var res1 = firstDoThis(calc, calc) var res2 = thenDoThis(res1) --- finallyDoThis(res2) } Use “do” expressions to prevent calculating a value multiple times, and reduce the nesting of function calls, increasing readability
  • 42. Creating Reuseable Functions ● Try to create functions that abstract over data types and not the data itself "It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures." —Alan Perlis
  • 43. Function Composition ● Small functions ● Wrap groupings of small functions in another function ● This is composition ● Promotes reuse
  • 45. Additional Info: Follow me on twitter: @jerney_io Learn how to use reduce (good content on HOFs as well): https://www.jerney.io/dataweave-when-to-use- reduce/ Looking for something more advanced? (pattern matching, recursion): https://www.jerney.io/dataweave- applywhenkey-function/, https://www.jerney.io/how-to-modify-all-values-of-an-element/
  • 46. Additional Info (Cont.) Videos are your thing? Check out this great talk on Functional Programming by Russ Olsen: https://www.youtube.com/watch?v=0if71HOyVjY Good Intro to Functional Programming: First 6 Chapters of “On Lisp”, by Graham: http://ep.yimg.com/ty/cdn/paulgraham/onlisp.pdf Another Good Intro: First 2 Chapters of “Structure and Interpretation of Computer Programs”, by Abelson, Sussman & Sussman: http://web.mit.edu/alexmv/6.037/sicp.pdf Something way more advanced: “Functional Programming in Scala” by Chiusana & Bjarnason: https://github.com/awantik/scala- programming/blob/master/Manning%20Functional%20Programming%20in%20Scala%20(2015).pdf

Hinweis der Redaktion

  1. More esoteric languages like Clojure, Racket, Scala, Ruby Saw no DataWeave experts: when I started using Mule dataweave was just coming out, people were skeptical to learn it because they were afraid it would get replaced again like Data Mapper did. After using it for a few months, I thought MuleSoft nailed it, didn’t see why it would ever be replaced. Dove in.
  2. What problem is it solving?
  3. What problem is it solving?
  4. Might not get it the first time around Jot down any questions or doubts you have! We will have plenty of time to answer them at the end
  5. Just to be 100% honest with you all: This isn’t really a talk about DataWeave, it’s a talk about functional programming. Just using DW to illustrate the points
  6. Evaluation of mathematica functions vs using statements to call methods
  7. Functions are completely deterministic
  8. An expression is something that always evaluates to a value. Expressions can always be replaced by the value that they evaluate to. This means everything except variable and function declarations return something in DataWeave. This has a profound affect on the way that you write your code.
  9. Quiz question, can you accomplish the DataWeave if/else expression in Java? How?
  10. NOTE: Update this slide, it’s confusing
  11. NOTE: Update this slide, it’s confusing
  12. Think of logical groupings as any piece of code you could put a set of parenthesis around
  13. Objects in the object oriented sense, not the JavaScript sense This is great because when you call a function in dataweave you never need to worry about if that function is going to change anything you passed into it.
  14. Can anyone name an exception to the last rule?
  15. Make sure you fully understand how they’re different from Java methods (pass functions around w/o classes, assign them to variables), and how to effectively use them (building higher-order functions, using lambdas). Higher-order functions are great for separating concerns. Could you imagine if you had to write the code to loop through an array and modify every value in a specific way every time? With the higher-order function map, you don’t have to.