Clojure - map-invert



反轉對映,使值成為鍵,反之亦然。

語法

以下是語法。

(map-invert hmap)

引數 - ‘hmap’ 是雜湊鍵和值的對映。

返回值 - 返回一個對映,其中值反轉為鍵,鍵反轉為值。

示例

以下是 Clojure 中 map-invert 的示例。

(ns clojure.examples.example
   (:require [clojure.set :as set])
   (:gen-class))
(defn example []
   (def demokeys (hash-map "z" 1 "b" 2 "a" 3))
   (def demonew (set/map-invert demokeys))
   (println demonew))
(example)

輸出

以上程式碼產生以下輸出。

{1 z, 3 a, 2 b}
clojure_maps.htm
廣告

© . All rights reserved.