This document provides an introduction to DataWeave, a functional programming language for data transformations. It outlines some key concepts of DataWeave including that (1) it uses expressions instead of statements, (2) data is immutable, and (3) functions are the main tool for computation. The document recommends fully understanding functions and higher-order functions to effectively use DataWeave. It provides some practical tips for pattern matching, avoiding repeated calculations, and creating reusable functions through composition.
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
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?
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.
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:
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
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.
What problem is it solving?
What problem is it solving?
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
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
Evaluation of mathematica functions vs using statements to call methods
Functions are completely deterministic
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.
Quiz question, can you accomplish the DataWeave if/else expression in Java? How?
NOTE: Update this slide, it’s confusing
NOTE: Update this slide, it’s confusing
Think of logical groupings as any piece of code you could put a set of parenthesis around
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.
Can anyone name an exception to the last rule?
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.