Lädt...


🔧 Elixir pattern matching - save your time and friendly with our thinking way!


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Intro

Once of interesting features of Elixir is pattern matching. That is way to save your time and similar the way we thinking.

Pattern matching is way to check equal value, extract value from tuple, list, map and binary.

How to uses

Pattern matching always return variable (term) for us can continue using it for other expression.

Three things we need to care in here:

  • If not match it will raise an error.
  • Pattern matching is always return original term (variable).
  • If we add a variable for extracting, after matched variable will have value follow format we expected.

No we go to two use cases for using pattern matching.

check similar(condition) value

This type of pattern matching can help us verify a value, format of variable/param.

re

Simple we can check a atom like:

a = :an_atom
b = 1_000
c = {:ok, 1}

# ...

# use case 1
:an_atom = a
1_000 = b
{:ok, 1} = c

# use case 2
case a do
 :an_atom ->
  :ok
 _other ->
  :nok
end

case b do
 1_000 ->
  :default
 _ ->
  :user_config
end

# use case 3
check_ok = fn 
  :ok -> true
  _ -> false
end

result = check_ok.(a)

For case check equal value, that usually uses for verifying value, condition for branch code in function/case do.

If variable is complex term example a tuple, list, map. We can discard other values to check one or more values we want.

Example:

list = [1, :a, "hello"]
tuple = {:ok, :a, 1}
map = %{a: 1, b: "hello"}

# check first item of list is 1.
[1|_] = list # or ^list = [1|_]

# check tuple has same values.
copy = ^tuple = {:ok, :a, 1}

# check tuple has same number of elements.
{_, _, _} = tuple

# check map has a key = :a and value of key = 1.
%{a: 1} = map

Extracting value

This feature of pattern matching help us can extract one or more value from a complex term.

Remember, our expected value will bind to variable follow format of our pattern or key in map case.

extract value

(expression of pattern matching:
{:ok, get_value} = {:ok, "hello"}.
after matched, we got "hello" value from original term)

Example:

tuple = {:ok, "hello"}
list = [:a, :b, 1, 2]
complex = {:ok, [{:first, 1}, {:second, [1, 2, 3]}]}

# extract second element of tuple
{:ok, value} = tuple

# get first item in list
[firt|_] = list

# get second item in list
[_, second|_] = list

# get value of result
{:ok, [{_, first_value}, {:second, second_value}]} = complex

We can use all key of example for function like:

defmodule Example do

  def return_second([_, second|_]) do
    # do something with second item.
  end

 def extract_map(%{a: value} = map) do
   # do some thing with value & map.
 end
end

works with binary/bitstring

This is interesting way to work with binary in Elixir. I wish other languages have this feature.

(I will add this later)

...

🔧 C# Pattern Matching Inside Out: Kompakter und prägnanter C#-Code durch Pattern Matching


📈 57.66 Punkte
🔧 Programmierung

🔧 Mastering Pattern-Matching in Elixir


📈 46.6 Punkte
🔧 Programmierung

🔧 Elixir Task, Task.Supervisor - Another way to work with Elixir process


📈 43.13 Punkte
🔧 Programmierung

🔧 Improve Your C# Code with Pattern Matching! 🚀


📈 32.1 Punkte
🔧 Programmierung

🔧 Debugging Tips and Tricks for Python Structural Pattern Matching


📈 30.46 Punkte
🔧 Programmierung

🔧 Mastering C# Switch Expressions and Pattern Matching: A Practical Guide


📈 30.46 Punkte
🔧 Programmierung

🔧 The Power of Small Tweaks: Java 17's Flow Scoping and Pattern Matching Unveiled


📈 30.46 Punkte
🔧 Programmierung

🔧 Pattern Matching and Records Changes in Java 21: Every Java Developer Must Know


📈 30.46 Punkte
🔧 Programmierung

🔧 Exploring Java Record Patterns and Pattern Matching


📈 30.46 Punkte
🔧 Programmierung

📰 YARA – Pattern Matching Tool For Malware Analysis


📈 28.83 Punkte
📰 IT Security Nachrichten

📰 YARA – Pattern Matching Tool For Malware Analysis


📈 28.83 Punkte
📰 IT Security Nachrichten

🐧 Bash script function 'ren' which rename in batch any file name matching the pattern of nearly/emulated PCRE


📈 28.83 Punkte
🐧 Linux Tipps

🎥 HPR2888: Pattern matching in Haskell


📈 28.83 Punkte
🎥 Podcasts

🔧 C# Language Highlights: Pattern Matching Basics | On .NET


📈 28.83 Punkte
🔧 Programmierung

📰 Programmiersprache: Ruby 2.7 führt Pattern Matching ein


📈 28.83 Punkte
📰 IT Nachrichten

📰 After 19 Years, Python May Finally Get a Pattern Matching Syntax


📈 28.83 Punkte
📰 IT Security Nachrichten

🐧 [$] "Structural pattern matching" for Python, part 1


📈 28.83 Punkte
🐧 Linux Tipps

🐧 [$] "Structural pattern matching" for Python, part 2


📈 28.83 Punkte
🐧 Linux Tipps

🐧 [$] Python structural pattern matching morphs again


📈 28.83 Punkte
🐧 Linux Tipps

🐧 Pattern matching accepted for Python


📈 28.83 Punkte
🐧 Linux Tipps

📰 Golang Approves Generics, While Python Accepts Pattern-Matching Proposals


📈 28.83 Punkte
📰 IT Security Nachrichten

🔧 C# Language Highlights: Tuple Pattern Matching | On .NET


📈 28.83 Punkte
🔧 Programmierung

🐧 svlogd pattern matching doesn't seem to work as documented


📈 28.83 Punkte
🐧 Linux Tipps

🐧 [$] An overview of structural pattern matching for Python


📈 28.83 Punkte
🐧 Linux Tipps

🔧 Expressive Code with Pattern Matching


📈 28.83 Punkte
🔧 Programmierung

🐧 [$] An alternate pattern-matching conditional for Elisp


📈 28.83 Punkte
🐧 Linux Tipps

🔧 Binary pattern matching <<1::1, 0::1, 1::1>>


📈 28.83 Punkte
🔧 Programmierung

🔧 Find What You Need: Pattern Matching in SQL


📈 28.83 Punkte
🔧 Programmierung

🔧 Rust Tutorial – Learn Advanced Iterators & Pattern Matching by Building a JSON Parser


📈 28.83 Punkte
🔧 Programmierung

🔧 Pattern Matching | Uzbek | C#


📈 28.83 Punkte
🔧 Programmierung

🔧 Pattern Matching | Uzbek | C#


📈 28.83 Punkte
🔧 Programmierung

🔧 Pattern Matching for Switch in Java 21


📈 28.83 Punkte
🔧 Programmierung

matomo