खोज…


पैटर्न मिलान कार्य

#You can use pattern matching to run different 
#functions based on which parameters you pass

#This example uses pattern matching to start, 
#run, and end a recursive function

defmodule Counter do
    def count_to do
        count_to(100, 0) #No argument, init with 100
    end

    def count_to(counter) do
        count_to(counter, 0) #Initialize the recursive function
    end

    def count_to(counter, value) when value == counter do
        #This guard clause allows me to check my arguments against
        #expressions. This ends the recursion when the value matches
        #the number I am counting to.
        :ok
    end

    def count_to(counter, value) do
        #Actually do the counting
        IO.puts value
        count_to(counter, value + 1)
    end
end

नक्शे पर पैटर्न का मिलान

%{username: username} = %{username: "John Doe", id: 1}
# username == "John Doe"
%{username: username, id: 2} = %{username: "John Doe", id: 1}
** (MatchError) no match of right hand side value: %{id: 1, username: "John Doe"}

एक सूची पर पैटर्न मिलान

आप एलिक्जिर डेटा स्ट्रक्चर्स जैसे लिस्ट्स पर भी मैच कर सकते हैं।

सूचियाँ

एक सूची पर मिलान काफी सरल है।

[head | tail] = [1,2,3,4,5]
# head == 1
# tail == [2,3,4,5]

के बाएं हाथ की ओर करने के लिए सूची में पहले (अधिक या) तत्वों का मिलान करके यह काम करता है | (पाइप) और सूची के बाकी दाहिने हाथ की ओर चर |

हम किसी सूची के विशिष्ट मूल्यों पर भी मेल कर सकते हैं:

[1,2 | tail] = [1,2,3,4,5]
# tail = [3,4,5]

[4 | tail] = [1,2,3,4,5]
** (MatchError) no match of right hand side value: [1, 2, 3, 4, 5]

बाईं ओर लगातार कई मूल्यों को बांधना | यह भी अनुमति है:

[a, b | tail] = [1,2,3,4,5]
# a == 1
# b == 2
# tail = [3,4,5]

और भी जटिल - हम एक विशिष्ट मूल्य पर मेल कर सकते हैं, और एक चर के खिलाफ मैच कर सकते हैं:

iex(11)> [a = 1 | tail] = [1,2,3,4,5]
# a == 1

पैटर्न मिलान का उपयोग करके एक सूची का योग प्राप्त करें

defmodule Math do
  # We start of by passing the sum/1 function a list of numbers.
  def sum(numbers) do
    do_sum(numbers, 0)
  end

  # Recurse over the list when it contains at least one element.
  # We break the list up into two parts:
  #   head: the first element of the list
  #   tail: a list of all elements except the head
  # Every time this function is executed it makes the list of numbers
  # one element smaller until it is empty.
  defp do_sum([head|tail], acc) do
    do_sum(tail, head + acc)
  end

 # When we have reached the end of the list, return the accumulated sum
  defp do_sum([], acc), do: acc
end

अनाम कार्य

f = fn
  {:a, :b} -> IO.puts "Tuple {:a, :b}"
  [] -> IO.puts "Empty list"
end

f.({:a, :b}) # Tuple {:a, :b}
f.([])       # Empty list

tuples

{ a, b, c } = { "Hello", "World", "!" }    

IO.puts a # Hello
IO.puts b # World
IO.puts c # !

# Tuples of different size won't match:

{ a, b, c } = { "Hello", "World" } # (MatchError) no match of right hand side value: { "Hello", "World" }

एक फ़ाइल पढ़ना

पैटर्न मिलान फ़ाइल रीडिंग जैसे ऑपरेशन के लिए उपयोगी है जो एक टपल लौटाता है।

यदि फ़ाइल sample.txt में This is a sample text , तो:

{ :ok, file } = File.read("sample.txt")
# => {:ok, "This is a sample text"}

file
# => "This is a sample text"

अन्यथा, यदि फ़ाइल मौजूद नहीं है:

{ :ok, file } = File.read("sample.txt")
# => ** (MatchError) no match of right hand side value: {:error, :enoent}

{ :error, msg } = File.read("sample.txt")
# => {:error, :enoent}

अनाम मिलान कार्यों का पैटर्न

fizzbuzz = fn
  (0, 0, _) -> "FizzBuzz"
  (0, _, _) -> "Fizz"
  (_, 0, _) -> "Buzz"
  (_, _, x) -> x
end

my_function = fn(n) ->
  fizzbuzz.(rem(n, 3), rem(n, 5), n)
end


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow