#解一元二次方程 getDelta = (a,b,c) -> b * b - 4 * a * c; getAnswer = (a,b,c) -> delta = getDelta a,b,c if delta < 0 then printAnswer null,null else if delta > 0 then printAnswer (-b + Math.sqrt delta)/2*a, (-b - Math.sqrt delta)/2*a else printAnswer -b/2*a printAnswer = (a, b) -> if not a? and not b? then "no answer" else if a? and not b? then "answer is: #{a}" else "answer is: #{a} and #{b}" getAnswer2 = (a,b,c) -> delta = getDelta a,b,c switch when delta < 0 then printAnswer null, null when delta > 0 then printAnswer (-b + Math.sqrt delta)/2*a, (-b - Math.sqrt delta)/2*a else printAnswer -b/2*a alert getAnswer2 1,4,1
#斐波那契数列 fabonacci = (index) -> switch index when 0 then 1 when 1 then 1 else (fabonacci index-1) + (fabonacci index-2) print = (num)-> (fabonacci num for num in [1..num]) alert print 3 array = (fabonacci num for num in [0..32]) alert array alert print 5 ---------------------------------------------------- fabonacciMap = [] addMap = (index, value) -> fabonacciMap[index] = value value fabonacci = (index) -> if fabonacciMap[index]? then fabonacciMap[index] else switch index when 0 then addMap index,1 when 1 then addMap index,1 else a = fabonacci index-1 b = fabonacci index-2 addMap index, a+b array = (fabonacci num for num in [0..42]) alert array
addMap 是副作用函数,纯粹的函数式语言貌似不允许写这种函数
#=====================get random sort========================================= fun = (total) -> [1..total] rand = (total) -> Math.random() * total // 1 randArr = (total) -> (rand num for num in [1..total]) getRandSort = (num) -> ret=[] arr = fun num while arr.length > 0 randNum = rand arr.length value = arr.splice randNum,1 ret.push value ret #alert "value:"+value+" randNum:"+randNum+"\n arr:"+arr alert getRandSort 5
#万年历 trace = () -> ret = "" ret += "," + val for val in arguments alert ret anchor = {today: "2014-11-30", weekDay:7} months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] getDateObj = (date) -> [year, month, day] = date.split "-" {year:parseInt year; month:parseInt month; day:parseInt day} isLeapYear = (year) -> if year%100 is 0 and year % 400 isnt 0 then false else if year % 400 is 0 then true else year % 4 is 0 getDays = (year) -> if isLeapYear year then 366; else 365 getDaysAmongYears = (smallYear, bigYear) -> needReverse = bigYear < smallYear if needReverse then [bigYear, smallYear] = [smallYear, bigYear] if bigYear is smallYear then 0 else ret = 0; ret += getDays year for year in [smallYear..bigYear-1] if needReverse then -ret else ret #leap month is not considered in this method getDaysAmongMonths = (smallMonth, bigMonth) -> needReverse = bigMonth < smallMonth if needReverse then [bigMonth, smallMonth] = [smallMonth, bigMonth] if bigMonth is smallMonth or bigMonth is 1 then 0 else ret = 0; ret += months[month] for month in [smallMonth-1..bigMonth-2] if needReverse then -ret else ret floorDate2Year = (date) -> obj = getDateObj date offset = getDaysAmongMonths(1, obj.month) + obj.day if isLeapYear(obj.year) and obj.month > 2 then offset += 1 {offset: offset, year: obj.year} getDiffDays = (smallDate, bigDate) -> big = floorDate2Year bigDate small = floorDate2Year smallDate yearDays = getDaysAmongYears small.year, big.year offset = big.offset - small.offset #trace big.offset, small.offset yearDays + offset getWeekDay = (date) -> diffs = (getDiffDays anchor.today, date) % 7 diffs += 7 if diffs < 0 switch diffs when 0 then "Sunday" when 1 then "Monday" when 2 then "Tuesday" when 3 then "Wednesday" when 4 then "Thursday" when 5 then "Friday" else "Saturday" #alert getDiffDays "2012-3-28","2013-3-28" alert getWeekDay "2012-2-29"
帮助: http://coffeescriptcookbook.com/chapters/arrays/creating-a-string-from-an-array