This is a follow-up from my previous Kata which can be found here: http://www.codewars.com/kata/5476f4ca03810c0fc0000098

This time, for any given linear sequence, calculate the function [f(x)] and return it as a function in Javascript or Lambda/Block in Ruby.

For example:

```python
get_function([0, 1, 2, 3, 4])(5) => 5
get_function([0, 3, 6, 9, 12])(10) => 30
get_function([1, 4, 7, 10, 13])(20) => 61
```

Assumptions for this kata are:
```
The sequence argument will always contain 5 values equal to f(0) - f(4).
The function will always be in the format "nx +/- m", 'x +/- m', 'nx', 'x' or 'm'
If a non-linear sequence simply return 'Non-linear sequence' for javascript, ruby, and python. For C#, throw an ArgumentException.
```

Source: codewars | Difficulty: EASY
Tags: Fundamentals

Mathematics

Starter code: def get_function(sequence):
	

Solutions:
def get_function(sequence):
	slope = sequence[1] - sequence[0]
	for x in range(1, 5):
		if sequence[x] - sequence[x - 1] != slope:
			return 'Non-linear sequence'
	return lambda a: slope * a + sequence[0]


def get_function(sequence):
	(first, dist) = (sequence[0], sequence[1] - sequence[0])
	valid = all((x == dist * i + first for (i, x) in enumerate(sequence)))

	def f(x):
		return dist * x + first
	return f if valid else 'Non-linear sequence'


def get_function(seq):
	b = seq[0]
	a = seq[1] - b
	f = lambda x: a * x + b
	if not all((f(i) == seq[i] for i in range(5))):
		return 'Non-linear sequence'
	return f


def get_function(lst):
	(a, b) = (lst[1] - lst[0], lst[0])
	f = lambda x: a * x + b
	return f if all((f(x) == y for (x, y) in enumerate(lst))) else 'Non-linear sequence'


def get_function(sequence):
	(s0, s1) = sequence[:2]
	slope = s1 - s0
	if any((b - a != slope for (a, b) in zip(sequence[1:], sequence[2:]))):
		return 'Non-linear sequence'
	return lambda x: slope * x + s0


def get_function(arr):
	if len(set((j - i for (i, j) in zip(arr, arr[1:])))) != 1:
		return 'Non-linear sequence'
	else:
		return lambda x: arr[0] + (arr[2] - arr[1]) * x


def get_function(a):
	d = [j - i for (i, j) in zip(a, a[1:])]
	return ['Non-linear sequence', lambda v: d[0] * v + a[0]][len(set(d)) == 1]


def get_function(sequence):
	res = lambda x: sequence[1] * x - sequence[0] * (x - 1)
	if any((res(x) - v for (x, v) in enumerate(sequence[2:], 2))):
		return 'Non-linear sequence'
	return res


def get_function(l):
	return 'Non-linear sequence' if any((l[i - 1] + l[1] - l[0] != l[i] for i in (2, 3, 4))) else lambda x: (l[1] - l[0]) * x + l[0] if l[1] - l[0] else l[0]


def get_function(seq):
	if len(set([seq[i + 1] - seq[i] for i in range(len(seq) - 1)])) != 1:
		return 'Non-linear sequence'
	return lambda x: x * (seq[1] - seq[0]) + seq[0]