我有:
(defrecord human-being [uuid first-name last-name genome-sequence])
(defrecord space-ship [uuid ship-type engines home-world captain])
我想要一个单一的建筑'将记录人类或太空船的记录并返回一组密钥作为实际记录密钥的函数:
(def john (human-being. "ABC123" "John" "Smith" "QWERTY"))
(def enterprise (space-ship. "ZXC123" "Galactic" "Warp" "Earth" "Picard"))
(constructFunc john) --returns--> {:uuid "ABC123" :first-name "John" :last-name "Smith" :genome-sequence "QWERTY"}
(constructFunc enterprise) --returns--> {:uuid "ZXC123" :ship-type "Galactic" :engines "Warp" :home-world "Earth" :captain "Picard"}
我不想要这两个散文。我需要能够放弃任何预定记录并得到类似的输出......
我有一种感觉,我应该为此使用宏,但这让我有点害怕......
答案 0 :(得分:1)
看到问题是“我如何将Defrecord变成地图”我可以想到4种或多或少的等价方式。
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as stats
import matplotlib.mlab as mlab
import math
# read data into a DataFrame
data = pd.read_csv('http://www-bcf.usc.edu/~gareth/ISL/Advertising.csv', index_col=0)
h = sorted(data.TV)
hmean = np.mean(h)
hstd = np.std(h)
h_n = (h - hmean) / hstd
pdf = stats.norm.pdf( h_n )
# plot data
f,ax1 = plt.subplots()
ax1.hist( h_n, 20, normed=1 )
ax1.plot( h_n , pdf, lw=3, c='r')
ax1.set_xlim( [h_n.min(), h_n.max()] )
ax1.set_xlabel( r'TV $[\sigma]$' )
ax1.set_ylabel( r'Relative Frequency')
ax2 = ax1.twiny()
ax2.grid( False )
ax2.set_xlim( ax1.get_xlim() )
ax2.set_ylim( ax1.get_ylim() )
ax2.set_xlabel( r'TV' )
ticklocs = ax2.xaxis.get_ticklocs()
ticklocs = [ round( t*hstd + hmean, 2) for t in ticklocs ]
ax2.xaxis.set_ticklabels( map( str, ticklocs ) )