假设我有一个包含元素的列表,例如[1,2,3,4,5,6,7,8].我想创建长度为N的这些元素的所有排列.
所以,因为N = 4它将是
[[1,1,1,1],[1,1,1,2],[1,1,2,1],[1,2,1,1],[2,1,1,1],..]等等.
comb :: Int -> [a] -> [[a]]
comb 0 _ = [[]]
comb _ [] = []
comb m (x:xs) = map (x:) (comb (m-1) xs) ++ comb m xs
Run Code Online (Sandbox Code Playgroud)
现在,为了首先得到这个列表,我找到了重复列表的所有组合,因此[[1,1,1,1],[1,1,1,2],[1,1,1,3],[1,1,1,4],[1,1,1,5],...],对于每个列表,我找到所有排列,如果排列不在列表中,我将它添加到列表中.
permutations1 :: Eq a => [a] -> [[a]]
permutations1 [] = [[]]
permutations1 list = [(x:xs) | x <- list, xs <- permutations1 $ delete1 x list]
delete1:: Eq a => a -> [a] -> [a]
delete1 …Run Code Online (Sandbox Code Playgroud) 我只是想找到Martin Fowler的Domain Model模式的一些例子而我不能.
从我在Internet上发现的域模型只是向类中添加一些"逻辑"方法.例如
public class Income {
private String title;
private String comment;
private String date;
private Double amount;
private Integer category;
public ExIn(String title, String comment, Double amount, Integer category, String date) {
this.title = title;
this.comment = comment;
this.date = date;
this.amount = amount;
this.category = category;
}
public Integer getCategory() {
return category;
}
public void setCategory(Integer category) {
this.category = category;
}
// more getters and setters
// Domain Model part starts
public …Run Code Online (Sandbox Code Playgroud) data Bst a = Empty | Node (Bst a) a (Bst a)
lElems :: Ord a => a -> Bst a -> Int
lElems _ Empty = -1
lElems n (Node l m r)
| n == m = n
| n > m = lElems n r
| n < m = lElems n l
Run Code Online (Sandbox Code Playgroud)
在这里,您可以看到类型和程序的定义.如果它在树中,我想返回元素,如果不是,则返回-1.
However, I had this problem
Couldn't match expected type `Int' with actual type `a'
`a' is a rigid type variable bound by
the …Run Code Online (Sandbox Code Playgroud) 我知道我在这里重新发明轮子,但我只是不喜欢django-friendship并且想自己编写它。
因此,我有自定义用户模型 Person
class Person(AbstractBaseUser, PermissionsMixin):
username = models.CharField(('username'), max_length=75, unique=True,
help_text=('Required. 30 characters or fewer. Letters, numbers and '
'@/./+/-/_ characters'),
validators=[
validators.RegexValidator(re.compile('^[\w.@+-]+$'),
('Enter a valid username.'), 'invalid')
])
....more other fields....
friend = models.ForeignKey('self', related_name="friends", default=None, null=True)
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,我有一个带有 self 或其他词 oneToMany 关系的外键。
这是我在 views.py 中添加朋友的内容
def add_friend(request, username):
if request.user.is_authenticated():
user = Person.objects.get_by_natural_key(username)
if user is not None:
user.friend = request.user /// here
return HttpResponseRedirect('/profile/')
Run Code Online (Sandbox Code Playgroud)
我正在获取我想通过用户名添加到我的朋友的用户,并将用户字段(朋友)设置为当前用户(请求用户)。但是,我认为我在这里做错了。
我试图用谷歌搜索它,但没有找到示例 ForeignKey('self')
所以,我希望用户在 EditText 中写入两个数字除以这个符号“:”。我已将 TextWatcher 添加到此 EditText,这样我就可以看到用户正在输入的内容。
因此,这就是我在 afterTextChanged 中的观察者中所拥有的。
@Override
public void afterTextChanged(Editable s)
{
String textToEdit = s.toString();
if (s.length()==2)
{
String h = s.toString();
h = h+":";
edit.setText(h);
edit.setSelection(edit.getText().length());
}
}
Run Code Online (Sandbox Code Playgroud)
当用户输入超过 2 位数字时,我会向 EditText 添加“:”。
我在这里遇到了问题。该EditText的输入类型是“数字”,因此只能是数字,“:”不是数字。
小例子:用户输入 24。afterTextChanged 获取它并添加设置“24:”。24 是一个数字,“24:”是一个字符串。
所以,现在我有“24:”,当我尝试从 EditText 中删除某些内容时,我收到致命错误。
还有其他方法可以在我的 EditText 中显示“:”吗?或者我做错了什么?